File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Class Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class Reference" Watch "Class Reference" New topic
Author

Class Reference

Amit Chowdhary
Greenhorn

Joined: Feb 10, 2001
Posts: 3
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

Joined: Jan 26, 2001
Posts: 158
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

Joined: Sep 29, 2000
Posts: 8521
Jyotsna,
Wow! Great answer!!


"JavaRanch, where the deer and the Certified play" - David O'Meara
Amit Chowdhary
Greenhorn

Joined: Feb 10, 2001
Posts: 3
Thanks Jyotsna. That was great explanation. It'll help me a lot, as i always make mistakes in object reference.
Jyotsna Clarkin
Ranch Hand

Joined: Jan 26, 2001
Posts: 158
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

Joined: Feb 10, 2001
Posts: 3
Jyotsna,
That�s okay, I understood the concept very well. Thanks!!!
regards,
Amit
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Class Reference
 
Similar Threads
I am getting clone() has protected access in java.lang.Object
Polymorphism understanding....
Consider this code
method invocation
Threads Using Static Method