permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method invoking 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 "Method invoking" Watch "Method invoking" New topic
Author

Method invoking

Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Prints DE
Prints AAA

Can someone tell me why the first program does not print DD--since its compile time reference variable is of type D. However the second program prints AAA....where the explanation of the answer says tht since a1 is of type A...so it will only go into Class A to look for method m1()


---<br />SCJP 1.4
Akshatha Nayak
Ranch Hand

Joined: Jul 15, 2004
Posts: 53
hi Akthari,

i think its coz class D is declared abstract .. check this code it now prints EA & DA ..
this what i think .. i'm just trying to help u.. i'm not sure abt the answer .. ok?



abstract class D {
String s1 = "D";
String getS1() {return s1;}
}
abstract class E extends D {
String s1 = "E";
String getS1() {return s1;}
}
class A extends E {
String s1 = "A";
String getS1() { return s1; }
}
class F {
public static void main (String[] s) {
E x = new A();
D y = new A();
System.out.print(x.s1 + x.getS1());
System.out.print(y.s1 + y.getS1());
}
}


Only those who will risk going too far, Can possibly find out how far one can go! <br />happiness is journey and not destination<br /> <br />A Nayak<br />----------------------------------<br />SCJP 1.4<br />SCWCD 1.3<br />SCBCD 1.3
chowdary Thammineedi
Ranch Hand

Joined: Aug 16, 2004
Posts: 126
The first thing to keep in mind is that anything created with the new operator is not resolved until runtime and Methods are bound dynamically.

In the first program consider this line

D x = new E(); System.out.print(x.s1 + x.getS1());

At compile time the compiler resolves the first part. That is x is a reference for an object of type D. The abstract class D is loaded by the compiler and x.s1 is also resolved(static binding). Methods are dynamically bound. so x.getS1() is not resolved at this time.

At run time class E is loaded when the VM encounters new E() statement. But by this time it's too late to change the value of x.s1. But x.getS1() is resolved and the getS1() method in class E is invoked. That method has knowledge of the s1 variable.(The s1 in E hides the s1 in D). So it returns E.


For the second program the explanation u provided is correct.
am i right?
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108

Then wht abt this program.....Inspite of having a method call and the object being of type C....it prints CBA
[ August 16, 2004: Message edited by: Murtuza Akhtari ]
chowdary Thammineedi
Ranch Hand

Joined: Aug 16, 2004
Posts: 126
Hello Akhtari,

Methods are bound at run time.Static methods are bound at compile time. JIT Compilation? Optimization?

C c = new C(); c.m(); B b = c; b.m(); A a = b; a.m();

Let's break this down.

C c --- The compiler is made to know c refers to an object of type C.
c.m() --- The static method m() in C is bound. Class C is loaded.
Since class C extends B class B is loaded. And since class
B extends A class A is loaded.
B b = c; --- upcasting. No problemo.
b.m() --- The static method m() in B is bound.
A a = b --- upcasting. No problemo.
a.m() --- The static method in A is bound.

At run time the respective methods are executed.
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Thanks a lot for your replies guys !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method invoking