File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Accessing overridden methods with supoerclss ref variable 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 » Java » Beginning Java
Bookmark "Accessing overridden methods with supoerclss ref variable" Watch "Accessing overridden methods with supoerclss ref variable" New topic
Author

Accessing overridden methods with supoerclss ref variable

Shaan patil
Ranch Hand

Joined: Dec 22, 2007
Posts: 58
HI,

We know that a superclass ref vaiable can access a overridden method.Then why is it that we canot access subclass instance vaiable from a superclass vaiable ??


"You cannot change the truth but truth can change you "
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Welcome to the Ranch.

The super keyword which allows one to access the superclass object [or maybe that part of the object which is inherited unchanged from the superclass] is directional. A subclass "knows" which superclass it inherits from. When you writeorthe subclass knows to look in Foo for its superclass implementation and in Bar Barbar and Barbarbar for its interfaces. Remember super always refers to what follows extends.

But there might be several classes which extend Foo, and several which implement Bar Barbar and Barbaranne. If you had a "sub" keyword, it would not "know" which subclass to look for. Even javadoc can only find "direct known subclasses" (look at non-final classes in the API specification and it says that quite often), not "all direct subclasses."

A subclass might also have additional fields which are not in the superclass; there is no way the superclass could "know about" them.

It would not work having a "sub" keyword. I hope I have explained why so you can understand it.

CR
 
 
subject: Accessing overridden methods with supoerclss ref variable
 
Similar Threads
Assigning a subclass to a superclass type
can one access private methods of a superclass with a sub class reference?
Inheriting a protected member
Restrictive Access modifier????
can static method overridden?