-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposition.java
More file actions
41 lines (41 loc) · 964 Bytes
/
composition.java
File metadata and controls
41 lines (41 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class composition {
public static void main(String[]args)
{
//composition= "part of" relationship b/w obj
//example:- chain is part of a bag
//allows complex obj to be constructed from smaller obj
bag b= new bag("safari","black","waterproof");
System.out.println(b.brand);
System.out.println(b.color);
System.out.println(b.type.bagtype);
b.open();
}
}
class bag{
String brand;
String color;
Type type;
bag(String brand,String color,String bagtype)
{
this.brand=brand;
this.color=color;
this.type=new Type(bagtype);//here type is a obj of class Type
}
void open()
{
this.type.open();
System.out.println("bag is open!");
}
}
class Type
{
String bagtype;
Type(String bagtype)
{
this.bagtype=bagtype;
}
void open()
{
System.out.println("You opened the chain");
}
}