This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Why is the output 10? Not 20? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is the output 10? Not 20?" Watch "Why is the output 10? Not 20?" New topic
Author

Why is the output 10? Not 20?

Ramanath Rao
Greenhorn

Joined: May 22, 2006
Posts: 3
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
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: 3344

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 ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Howdy Srinivasan M,

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!

You can read about the naming policy here.

You can do the changes by editing your profile.

Thank you.
Ramanath Rao
Greenhorn

Joined: May 22, 2006
Posts: 3
Thanks Srinivasan for the reply. I got it.

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
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: 3344

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.
 
jQuery in Action, 2nd edition
 
subject: Why is the output 10? Not 20?