Click here to Skip to main content
16,018,264 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi...

i was trying to pass class as argument...

But i.m facing problem,concept not dot clear...

code stuff

Java
/* Class Test*/
class Test{
int x,y;
 Test(int x,int y){
    this.x=x;
    this.y=y;
  }
 void m3(){
   System.out.println("Test's m4");
 }
}

/*class Box*/
class Box
{
	int w,h,d;
	Box(int w,int h,int d){
	   this.w=w;
	   this.h=h;
	   this.d=d;
	}
	void m1(){
	  System.out.println("Box's m1");
	}
}

/*sub class of Box*/
class BoxWt extends Box
{
	int w,h,d,wt;
	BoxWt(int w,int h,int d,int wt){
	    super(w,h,d);
		this.wt=wt;
	}
	void m2(){
	System.out.println("BoxWt's m2");
	}
}

/*Figure Abstract class*/
abstract class Figure{
 int x,y;
 Figure(int x,int y){
  this.x=x;
  this.y=y;
 }
 abstract double area();
}
class Tri extends Figure
{
  int x,y;
  Tri(int x,int y){
  super(x,y);
  }
  double area(){
    return (0.5*x*y);
  }
}

/*Class Hello*/
class Hello{
  Test m4(Box z){
    System.out.println("M4,myreturn type is test");
	Box b=new Box(1,2,3);
	return b;
  }
 /* Box m5(int x,int y,int z){
     System.out.println("m5,my return type is Box");
	 return 1;
  }*/
  void m6(BoxWt z){
    System.out.println("m6,....................");
  }
  BoxWt m7(int x,int y,int z,int wt){
    BoxWt bw=new BoxWt(x,y,z,wt);
	return bw;
  }
}

/*main class*/
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);
	}
}

Pls help me to sort out..

Thank u..
Posted
Updated 10-Sep-12 2:36am
v2
Comments
Manfred Rudolf Bihy 10-Sep-12 9:15am    
And what would be your question if I may ask?
Unclear!

1 solution

Your main already shows you what to do:
Java
/*main class*/
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!

Java
Box b=new Box(1,2,3);

Test t=new Test(b); // b was passed as an argument to the object Test.


You will soon find out that Test needs to be capable to handle the argmuent of type Box:

Java
class Test{
  private Box oBox; // member variable in class Test
  
  public Test(Box aBox){
    this.oBox = aBox; // handover of argument to member variable which can now be used in class
  }

}

The simplest version of a object as argument is a setter. The direct opposite of it is a getter, which returns back an Object:

Java
// in class Test
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900