File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reference types SCJP 1.4

 
vandu matcha
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Srinivas Tuta
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u srinivas and sethi....
 
Tilo Hemp
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry ..it was a mistake..not chaining of constructors..but chaining of methods...i mistyped..sorry sorry..for the mistake
 
Tilo Hemp
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic