This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Polymorphic mechanism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Polymorphic mechanism" Watch "Polymorphic mechanism" New topic
Author

Polymorphic mechanism

Mukul Anand
Greenhorn

Joined: Nov 28, 2012
Posts: 29
Suppose we have two classes A and B.
B inherits from A.
Now see this :


My question is : Can we call all the methods in class B on reference obj??
Or are we restricted to call only those methods which are there in class A ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43905
    
  32
That is not polymorphism, but you are right. You can only call the methods which are in A.
Mukul Anand
Greenhorn

Joined: Nov 28, 2012
Posts: 29
So there is a downside of having a superclass reference to your subclass object. You can call only the inherited methods and not your own specific methods. Right?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43905
    
  32
Now how on earth is the compiler to tell whether it can call the fizz method or the buzz method? It is impossible to tell what the runtime type of sup will be in advance.

You are right: you can only call methods which are declared in the type of the reference. The reference type might be abstract or an interface, in which case the methods might be abstract in the reference type, but must be implemented in the runtime types.
 
Have you checked out Aspose?
 
subject: Polymorphic mechanism
 
jQuery in Action, 3rd edition