Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance - Mock Question & Explanation on Dan Chisholm's website.

 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Please refer question 20: http://www.danchisholm.net/july21/topic/section6/inherit1.html

Question 20

class A {void m1() {System.out.print("A");}}
class B extends A {void m1(){System.out.print("B");}}
class C extends B {void m1() {System.out.print("C");}}
class D extends C {
void m1() {System.out.print("D");}
void m2() {
m1();
((C)this).m1(); // 1
((B)this).m1(); // 2
((A)this).m1(); // 3
}
public static void main (String[] args) {
new D().m2(); // 4
}}

What is the result of attempting to compile and run the program?

a. Prints: DCBA
b. Prints: DDDD
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Run-time error
h. None of the above

Answer to Question 20: = b Prints: DDDD
http://www.danchisholm.net/july21/topic/section6/inherit1ans.html

------------------------------------------------------------------

My question is doesn't "((C)this).m1();" would have cast the D object into a C object? If so, why can't the answer be "DCBA"?

Thank you.
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


[edited to disable smilies]
[ July 03, 2006: Message edited by: Rick Reumann ]
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, dense here =) Can you elaborate on the code above and how it answers my question? Thank you.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriden methods are resolved at runtime based on the object's instance type.

((C)this).m1(); // 1
((B)this).m1(); // 2
((A)this).m1(); // 3

"this" refers to the instance of class D so the m1() method of class D is called.
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" refers to the instance of class D but why is it that "(C)this" did end up casting the instance "D" into an instance "C"?

--------------------

((C)this).m1(); // 1
((B)this).m1(); // 2
((A)this).m1(); // 3
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the object's instance type, for "((C)this)", is still class D.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't matter whether it's called a D or a C, the object is a D, and the method associated with D will be called.
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone. Now I finally got it =)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic