Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Reference

 
Amit Chowdhary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I'm executing this code and the result that i'm getting is different than what i was expecting. can some help me with this.
---------------
class ClassRef{

public static void main(String a[]){
Test2 t2 = new Test2();
Test1 t3 = t2;
System.out.println("t2.s: "+ t2.s);
System.out.println("t3.s: "+ t3.s);

t2.method();
t3.method();
}
}
class Test1{
String s ="test1";
void method(){
System.out.println("test1Method");
}
}
class Test2 extends Test1{
String s ="test2";
void method(){
System.out.println("test2Method");
}
}
--------------
o/p is:
t2.s: test2
t3.s: test1
test2Method
test2Method
-------
I was expecting:
t2.s: test2
t3.s: test2
test2Method
test2Method
-------
I'm still thinking that after this line of code
Test1 t3 = t2;
t3 refers to Test2 class instance and should give
t3.s value as "test2".
Thanks
 
Jyotsna Clarkin
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When dealing with classes & inheritance remember the following:
It is important to understand the difference between
(a) invoking a method on an object AND
(b) accessing instance variables of an object.
The first is dynamic lookup . It means that when you call a METHOD using the object reference, then it is the class of the object that will determine the method executed.
The second is variable shadowing . This means that whenever a VARIABLE is accessed using the object reference then it is the type of the reference that will determine the variable accessed.
So let's consider your code
Test1 t3 = t2;
Here the reference type is Test1
But the class of the object is Test2 created in previous statement new Test2();
All you have done is assign object of type Test2 to reference of Test3. This is legal because Test2 is a subclass of Test3.
t3.method() will call METHOD in Test2
dynamic lookup because object belong to class Test2
And t3.s will access VARIABLE String s in Test1
variable shadowing because reference t3 is of type Test1
You could play around with these concepts by writing some code. It will help you understand.
regards,
Jyotsna
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jyotsna,
Wow! Great answer!!
 
Amit Chowdhary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jyotsna. That was great explanation. It'll help me a lot, as i always make mistakes in object reference.
 
Jyotsna Clarkin
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Amit,
I made a small mistake in following sentence.
All you have done is assign object of type Test2 to reference of Test3. This is legal because Test2 is a subclass of Test3.
Test3 should be Test1 in the above sentence. Please make the correction.
Thank you (& Cindy) for the pat on the back.
regards,
Jyotsna
 
Amit Chowdhary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jyotsna,
That�s okay, I understood the concept very well. Thanks!!!
regards,
Amit
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic