aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan exam doubt 17 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan exam doubt 17" Watch "Dan exam doubt 17" New topic
Author

Dan exam doubt 17

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
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 ?


Thanks and Regards, Amit Taneja
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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.


SCJA 1.0 (98%), SCJP 1.4 (98%)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
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

Joined: Dec 29, 2004
Posts: 243
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

Joined: Mar 01, 2005
Posts: 485
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

Joined: Jun 25, 2004
Posts: 101
Sanju,

Thanx for such a Smart Example.

- jibiN

have will, have everything...


<b>J Abraham</b>
SCJP5,SCWCD1.4
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
Hi Parameswaran,

You are right. But the static methods are implicitly final, so there is no concept of overriding.
 
wood burning stoves
 
subject: Dan exam doubt 17