• 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
  • 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

Dynamic Binding

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class B{
int x;
B(){ x = 10;}
void m1(){x = 20;}
void m1(int x){this.x = x;}
}
class A extends B{
int x;
A(){x = 20;}
void m1(int x){super.x = x;}
public static void main(String arf[]){
B x = new A();
System.out.println(x.x);
x.m1();
System.out.println(x.x);
x.m1(30);
System.out.println(x.x);
((A)x).m1(30);
System.out.println(x.x);
}
}
a. 10 20 20 20
b. 10 20 30 30
c. 20 20 20 30
d. 20 20 20 20
e. Compiler error or run time error.
f. None of the above, it will give another output.

Answer is given as b, can anyone explain pls.
-Arun
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The bottom line here is that field (member variable) access is done according to the declared type of the variable (on the left of the assignment) while methods are resolved according to the runtime type of the object (on the right of the assignment).
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that A extends B, so A inherits m1() and overrides m1(int) from A. The methods will be picked up based on signature and object type.
Let's keep a record of A.x and B.x values after each statement:
>>> A.x = 0, B.x = 0 //class initialization
B(){ x = 10;}; // This implicit super() call is invoked every time a new A() is created.
>>> A.x = 0, B.x = 10
B x = new A();
>>> A.x = 20, B.x = 10
System.out.println(x.x); >>> prints 10 (B.x) because x is a supertype type. Variables are picked up strictly based on reference type.
x.m1(); // m1 in B is invoked because it matched the signature.
>>> A.x = 20, B.x = 20
System.out.println(x.x); >>> prints 20 (B.x) because x is a supertype type. Variables are picked up strictly based on reference type.
x.m1(30); // m1 in A is invoked because it matched the signature, and the method is non-static and overriding. Dynamic method binding applies: Method is picked up based on object type.
>>> A.x = 20, B.x = 30
System.out.println(x.x); >>> prints 30 (B.x). Variables are picked up strictly based on reference type.
((A)x).m1(30); // The casting changes the reference type from supertype to subtype. m1 in A is invoked because of its object type, not because of the casting.
>>> A.x = 20, B.x = 30
System.out.println(x.x); >>> prints 30 (B.x) because x is a supertype type. Variables are picked up strictly based on reference type. The casting in the preceding statement is transient, does not hold through the following statement.
[ April 16, 2002: Message edited by: Doanh Nguyen ]
[ April 16, 2002: Message edited by: Doanh Nguyen ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic