Your main already shows you what to do:
class ExDemo{
public static void main(String[] args){
Box b=new Box(1,2,3);
BoxWt bw=new BoxWt(4,5,6,7);
Test t=new Test(10,20);
Hello h=new Hello();
Figure f;
f=new Tri(30,40);
}
}
You are passing
int
-values to the objects(*).
SO if you want to pass an object as a argument to another object - do it the same way!
Box b=new Box(1,2,3);
Test t=new Test(b);
You will soon find out that
Test
needs to be capable to handle the argmuent of type
Box
:
class Test{
private Box oBox;
public Test(Box aBox){
this.oBox = aBox;
}
}
The simplest version of a object as argument is a setter. The direct opposite of it is a getter, which returns back an Object:
public Box getBox(){
return oBox;
}
Please also check out this:
The Java Tutorials: Object-Oriented Programming Concepts[
^] for more and detailed information.
* We do not talk much about classes, it's usually an object.