aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reference types SCJP 1.4 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 "reference types SCJP 1.4" Watch "reference types SCJP 1.4" New topic
Author

reference types SCJP 1.4

vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
this is a question from a practise test(danchisholm)..
code goes as follows:

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?
ans is DDDD
but as we used this reference and D is the subclass of A,B,C the output should be DCBA....but why it is DDDD...even though at runtime the object is D..it is cast to D's superclasses..i am a bit confused..ranchers please help
Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91
this keyword is used to access data in same class and super keyword is used to access data in super class. and since you are using this keyword. m1() of class D is being invoked everytime.

thanks


thanks,<br />Anju Sethi
Srinivas Tuta
Greenhorn

Joined: Oct 12, 2005
Posts: 10
In method m2() of class D, the methods being invoked at runtime are of current object (since this), which means everytime method m1() of the instance of class D is invoked. Since D is derived from C and also from B, A indirectly, the its current object can actually be referred by their super class reference types and hence typecasting to C , B, A is allowed. But at runtime the object being passed is this, means current object.


Srinivas
SCJP1.4
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
thank u srinivas and sethi....
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91
just curious... would it be possible to call A's m1() directly from within class D, without instantiating class A. i mean, by super.super.super.m1() or the like; I tried it and super.super.m1() does not work, but maybe there is another way?

- tilo
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
no..it is not possible to call a method in A without the instance of A..there is no other way ...
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91
hi vandu,

i was just playing around, and maybe i found a mechanism... although it might be so strange that it will not be used in a real program:

class B is defined as:

class B{
void m1(){
if(this instanceof D)
super.m1();
else
System.out.println("B");
}
}

(class C according)
and in D you just call super.m1().



cheers
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
yaah..this is chaining of constructors.....u are right..but i thought of only directly invoking.....i didnt think beyound it.....good work......and thank u for sharing...
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
sorry ..it was a mistake..not chaining of constructors..but chaining of methods...i mistyped..sorry sorry..for the mistake
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91
yeah, you're right, this is no direct invocation, but at least it is an invocation of the original method (which was overwritten several times) without instantiation
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: reference types SCJP 1.4