Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan exam doubt 17

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi in one of Dan quesion in oops chapter
as follow

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


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


the answer of above is "b"

with explanation as ...

The instance method that is invoked depends on the run-time type of the object--not the compile-time type of the reference. In each case, the method m1 is invoked on an object of type D; so the implementation of m1 in type D is selected each time.


I didn't understand why not "a" option is right ?
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this references an object of type D. Regardless of what reference type you cast this to, the result will still reference the D object. When it comes to deciding which method to invoke in this situation, the compiler uses the method that matches the type of the object, not the type of the reference. In all four cases, the object is an instance of D, even though the reference has been cast to various superclass types.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i did'nt get your answer

why in this question...of DAN the answer is "b"

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

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

a. Prints: D,D,D,D
b. Prints: D,C,B,A
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. Compile-time error at 5.
h. Run-time error
i. None of the above

???

please exp
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit,

The reason is data is shadowed rather than overridden. So we can remove the shadow through casting. But it is not possible in the case of overriding.

see the example code, that will give you a clear picture.


[ April 21, 2005: Message edited by: Sanju Thomas ]
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi thomas

when it is coming to overridden method look for the type of the object.
for overload method,overshadowed variable and static variable,static method go for the reference variable type.

am i correct
 
J Abraham
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanju,

Thanx for such a Smart Example.

- jibiN

have will, have everything...
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Parameswaran,

You are right. But the static methods are implicitly final, so there is no concept of overriding.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic