The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Methods are overridden, variables are shadowed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Methods are overridden, variables are shadowed" Watch "Methods are overridden, variables are shadowed" New topic

Methods are overridden, variables are shadowed

Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
I'm a bit confused on this topic, specially in shadowing variables.

output: 20 and 5
I know that method will be choosed at runtime(object type).
but varaiables? All classes has a variable named x.
The call of x depends on type or what?

Thank you.

SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Well actually your question is two fold.

shadowing is mostly when you have an instance variable x in the class, then in a method you declare a variable called x. So inside the method whenever you just use x, it will refer to the variable defined inside the method and not the instance variable. In order to access the instance variable you need to use the keyword this, as in this.x

No a subclass overriding an instance variable is like shadowing. But does refer to the variable type not the actual object type.

so Parent p = new Subclass();

When you refer to p.instance variable I believe you get the parent's instance variable value. But I could be wrong, because I always get confused on this point all the time myself.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Hi Adil,
Well yes, method is chosen at runtime but variables are shadowed or hidden.

If A is the base class and B is a subclass of A and both have a variable named say x, then the variable accessed will depend on the Type of the object that accesses that variable.

Therefore, in your code, the variable x will be accessed from class A

Hope this helps

<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Thank you for the reply .

Still confused! the returnIt() method will be choosed at runtime but as you can see this method return a shadowed variable x that it is defined in the superclass and the subclass too.

Result: the x returned by the method is defined in the subclass (object type not reference type!).
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
Instance variables cannot be overridden, ever. So the decision about which instance variable to access in any given situation is always based on reference type. Since the reference type for a is A, every time you access a.x no matter what the underlying object is, you will always get 5.

Methods on the other hand can be overridden, and resolution is made at runtime based on the underlying object type. Since a is actually pointing to an instance of B, it is B's returnIt() method that gets called, hence the result of 20.
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
Hi Adil,

variable selection depends upon reference type

method selection depends upon object type

hope this helps

Regards<br /> <br />Arvind Giri<br />MCA,SCJP 1.4,SCWCD 1.4<br />Looking for SCDJWS
I agree. Here's the link:
subject: Methods are overridden, variables are shadowed
It's not a secret anymore!