*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes INSTANCE VARIABLES 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "INSTANCE VARIABLES" Watch "INSTANCE VARIABLES" New topic
Author

INSTANCE VARIABLES

karthick devaraj
Greenhorn

Joined: Nov 17, 2008
Posts: 6
can anyone tell me the following answer?give me detailed reson?
class A
{
int x=10;
}
class B extends A
{
int x=20;
}
class Test
{
public static void main(String args[])
{
A a=new B();
System.out.println(a.x);
}
}
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

The answer is :10
May be you are expecting it to print 20....Even if the object is of the sub classs type.
The reason is because the base class reference variable CANNOT refer the members of the subclass(unless overridden).


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Mahrez Marouani
Greenhorn

Joined: Nov 18, 2008
Posts: 1
The reason is: Polymorphism applies only to methods and not to instance variables.
Henry Zhi Lin
Ranch Hand

Joined: Nov 04, 2008
Posts: 69
The unlike override method, instance variable determined at compilation times not runtime.



a is declared of type A so a = 10 not 20


SCJP 5.0
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Originally posted by Mahrez Marouani:
The reason is: Polymorphism applies only to non-static methods and not to instance variables.

[ November 18, 2008: Message edited by: Wouter Oet ]

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

I want to know the reason of one doubt.

When we extend class B by inheriting class A we also incorporate the variables. Now when are we redefining the variables then why the compiler is not giving any error? is not like defining same variable two times?

Please help.

Thanks in advance.


My Blog SCJP 5 SCWCD 5
Fabio Nascimento
Greenhorn

Joined: Nov 16, 2008
Posts: 16
Yeah but, theres no overriden variables so you always access variables of your reference type and not of your object.

A a = new B(); // B extends A

Any access to "a.anyVariable" will reffer to the A class. Any access to "a.anyMethod" at compile time will reffer to the A class. But with methods the polymorphism get in action at runtime and if you have an overriden method in B, then in the runtime it will execute the B's method.

Got it?

To see that in action, declare an y variable in B class and try to access it with "a.y", you will get a "cannot find symbol" compile error, since the compiler will look for "y" in the "A" class (which is the reference type)
[ November 18, 2008: Message edited by: Fabio Nascimento ]
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Thanks Fabio. I got it. The explanation was good.
 
Don't get me started about those stupid light bulbs.
 
subject: INSTANCE VARIABLES
 
Similar Threads
Private Field
explain
pls clarify
[constructor]...
ObjectInput and OutputStreams