aspose file tools*
The moose likes Java in General and the fly likes Calling Overridden methods and overshadowed variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling Overridden methods and overshadowed variables" Watch "Calling Overridden methods and overshadowed variables" New topic

Calling Overridden methods and overshadowed variables

Stephen Joseph
Ranch Hand

Joined: Dec 29, 2000
Posts: 50
Why is there difference between calling the overidden methods and overshadowed variables based on the object type(class) and reference (as stated in the book of Khalid )
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
I am not sure what you mean.But if you mean something that involves the code:
class Base{
String str="in Base";
void amethod()
{ system.out.println("in Base");

clas Sub extends base{
String str="in Sub";
void amethod()
System.out.println("In Sub");
class test{
public static void main(String args[])
Base b;
Sub s=new Sub();

the output is: in Sub //amethod of Sub invoked by s
in Sub //varaible str of Sub accessed by s
in Sub //amethod of Sub invoked by b
in Base //varaible str of Base accessed by b
So the method called depends on the object, not on the object reference . Here the object reference of type Base points to an object of type Sub. So method amethod of class Sub is invoked.But variables accessed depends on the object reference.Since the object reference is of type Base, the variable 'str' of the Base class is accessed.
I hope this is what you wanted to know

Author of JPhotoBrush Pro (
Stephen Joseph
Ranch Hand

Joined: Dec 29, 2000
Posts: 50
Thank you Tanveer.You guessed my Q right, but if you can tell me what is the reason for this difference why cant overridden methods and overshadowed variables be called in the same way.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Since this topic is not concerned with Threads and Synchronization, I am moving it to a more appropriate forum - Java in General(intermediate). Please continue the discussion there.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Think of it this way:
Class variables related to the state of the class
Class methods relate to the implementation of the class
The only reasons to extend one class with another is to:
1) Change state information, or
2) Change implementation
When we extend one class with another we automatically have access to the extended class state (non-private member variables). It would not be in our best interest to create an exact copy of the extended class state because we can just alter the extended class state. Therefore, Java developers made the statement that member variables (state) can not be overridden by subclasses. This makes sense since it would be time wasting and memory inefficient to inherit state and then try and duplicate it inside the new class! If we want to change state, then just change the inherited variables ...
On the other hand, we have extended the class for some reason and that had better be for one of the two reasons stated above. Java allows us to create numerous new class variables to extend our class state. We can override methods because that is usually the main reason for extending another class. This makes sense and doesn't cause programming inefficiencies!
I agree. Here's the link:
subject: Calling Overridden methods and overshadowed variables