• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Method invoking

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your replies guys !!!
reply
    Bookmark Topic Watch Topic
  • New Topic