Thursday, 8 October 2015

Java Program - Relationship between Objects


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Sec obj1 = new Sec(20);
Demo obj2 = new Demo(obj1);
obj2.display();
}
}


public class Demo {
Sec t;
int x;
Demo(Sec t){
this.t=t;
x=10;
}
void display(){
System.out.println("The value of x is " +x);
System.out.println("The value of y in Other object is " +t.i);
}
}


public class Sec {
int i;
Sec(int i){
this.i=i;
}
void display(){
System.out.println("The value of i is " +i);
}
}


output:-
The value of x is 10
The value of y in Other object is 20

No comments:

Post a Comment