programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
class B{
protected int x;
B(){
x=10;
}
B(int a){
x=a;
}
void ml(){x=20;}
void ml(int x){this.x=x;}
}
public class A{
int x;
A(){
x=20;}
A(int x){this.x=x;}
void ml(B x){
this.x=x.x++;
}
Public static void main(String args[]){
A x=new A(20);
B y=new B();
S.o.p(x.x);
x.ml(y);
S.o.p(x.x);
x.ml(30);
S.o.p(y.x);
((A)x).ml(y);
S.o.p(y.x);
}
}
ans is 20,10,30,31,pl explain

Ranch Hand
Posts: 42
• Number of slices to send:
Optional 'thank-you' note:
Dear Anu,
In main method,
In place x.ml(30) it should give y.ml(30)..to get result..
if it is x.ml(30)//compile error will come.. because there is no method A which takes int as argument..
A x=new A(20); //Here you are creating object of type A and x value is initialized to 20(x.x=20)
B y=new B();// Here you are creating object of type B calling default constructor which sets y.x=10.
S.o.p(x.x); // it will print 20..
x.ml(y);// Here you are passing Instance of object B(i.e) as argument to method m1 on x(instance of object A)..
it calling method..
void ml(B x){
this.x=x.x++;
}
above expression is equivalent to because we are passing (i.e ml(y))
now y.x=10
// this.x=y.x++; here the value of y.x(i.e 10) is assigned to
this.x ..
after this statemetn y.x=11(because it is incremented)
S.o.p(x.x)So it prints 10..ok
S.o.p(y.x) it will print 11 //check it
y.ml(30).. if you call this method then it assing y.x=30;
S.o.p(y.x); so it prints 30 here/
Now y.x=30//
((A)x).ml(y);
same as above I explained
this.x=y.x++, this.x=30 and y.x is increment so y.x=31/
S.o.p(y.x); //prints 31.