This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance - Mock Question & Explanation on Dan Chisholm's website. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance - Mock Question & Explanation on Dan Chisholm Watch "Inheritance - Mock Question & Explanation on Dan Chisholm New topic
Author

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

Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
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

Joined: Apr 03, 2001
Posts: 281


[edited to disable smilies]
[ July 03, 2006: Message edited by: Rick Reumann ]
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
Sorry, dense here =) Can you elaborate on the code above and how it answers my question? Thank you.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
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

Joined: Jun 18, 2006
Posts: 112
"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

Joined: Feb 02, 2006
Posts: 2023
But the object's instance type, for "((C)this)", is still class D.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jun 18, 2006
Posts: 112
Thank you everyone. Now I finally got it =)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance - Mock Question & Explanation on Dan Chisholm's website.
 
Similar Threads
Dan exam doubt 17
overriding ignores cast ?? Please explain
reference types SCJP 1.4
super of super
Nested Class - Mock Question & Explanation on Dan Chisholm's website.