File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please help" Watch "please help" New topic
Author

please help

Anu Kris
Greenhorn

Joined: Oct 01, 2001
Posts: 13
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
suresh seeram
Ranch Hand

Joined: Oct 06, 2001
Posts: 42
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.

I hope this will help you
regards
Suresh
Anu Kris
Greenhorn

Joined: Oct 01, 2001
Posts: 13
thanks for the help suresh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help