This week's book giveaway is in the JDBC forum. We're giving away four copies of SQL Antipatterns: Avoiding the Pitfalls of Database Programming and have Bill Karwin on-line! See this thread for details.
class A{ int t=10; } class B extends A{ int t=20; } public class TestSuper{ public static void main(String arg[]){ A a=new B(); System.out.println(a.t); } }
Srinivasan Mahadevan
Greenhorn
Joined: Jun 21, 2002
Posts: 5
posted
Please note that overriding is applicable only for instance methods. In case of variables, overriding is not applicable and field hiding is only applicable. A subclass can only hide the fields of a superclass and it cannot override them. Also note, when you access the field of an object, the type of the reference (here it is object A) will be accessed and not the class of the object which is denoted by the reference (here it is object B). Hence, 10 is outputted.
Generally, note that overriding is applicable only for instance methods. Only in that case, you need to see which object is actually being referred in runtime.
In all other cases (instance variables, static variables and static methods), overriding is not applicable and only hiding happens. In this case, the type of the reference will be accessed and it will be resolved during compile time itself.
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3057
posted
Howdy Ramanath Rao ,
Welcome to JavaRanch Hope Srinivasan's answer will clear your doubt.
What he meant to say about "Reference Type" and the "actual instance type" is, the classname is nothing but the reference type (the type of the reference variable) and the actual object which is created using "new".
Here a is of type A (means "a" is a variable of class A, in this case, it is a reference variable). The actual object being created is on type B (where you specify "new B()").
Hope this helps! [ January 07, 2008: Message edited by: Raghavan Muthu ]
Thanks for the reply and contribution. Keep up the good work!
You got to adjust your display name according to the ranch's naming policy which insists the users to have the first and last name separated by spaces (the names should be *real* names). As such you can expand the initial "M" i guess!
Originally posted by Srinivasan M: Please note that overriding is applicable only for instance methods. In case of variables, overriding is not applicable and field hiding is only applicable. A subclass can only hide the fields of a superclass and it cannot override them. Also note, when you access the field of an object, the type of the reference (here it is object A) will be accessed and not the class of the object which is denoted by the reference (here it is object B). Hence, 10 is outputted.
Generally, note that overriding is applicable only for instance methods. Only in that case, you need to see which object is actually being referred in runtime.
In all other cases (instance variables, static variables and static methods), overriding is not applicable and only hiding happens. In this case, the type of the reference will be accessed and it will be resolved during compile time itself.
Ramanath Rao
Greenhorn
Joined: May 22, 2006
Posts: 3
posted
Thanks Muthu for welcoming me.
I got the answer.
Thanks once again
Cheers, Ramanath
Originally posted by Raghavan Muthu: Howdy Ramanath Rao ,
Welcome to JavaRanch Hope Srinivasan's answer will clear your doubt.
What he meant to say about "Reference Type" and the "actual instance type" is, the classname is nothing but the reference type (the type of the reference variable) and the actual object which is created using "new".
Here a is of type A (means "a" is a variable of class A, in this case, it is a reference variable). The actual object being created is on type B (where you specify "new B()").
Hope this helps!
[ January 07, 2008: Message edited by: Raghavan Muthu ]
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3057
posted
Ramanath,
You do NOT need to quote the source for just adding your reply. You can just click on the 'Post Reply' button in the bottom of the page.