File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning subclass obj to superclass ref

 
shree vijay
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Consider the following code:

The difference between Java and C++ is that when you assign a derived class (subclass) object to the a reference of the base (superclass), Java remembers the original type of the object while C++ does not.
In the above example display() method is available in both Base and Derived. So, after the assignment b=d, the call to b.display() results in a call to the display() method in Derived.(it would have been the display() in the Base if it were C++).
But what happens when i call b.uniqueDisplay()?
I expected it would call that function from the Derived class normally. Any help?
Rgds,
Shree
 
Milind Deodhar
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the code and it gives a compile error. I have read in Java handbook by Patrick Naughton, but that was about interface
if we defined
interface i = Class c (assuming class implements interface)
even if the actual class is c, it will be able to reference methods , which are defined in interface and Class c implements it.
Here also, since there is a subclassing, I think the reference b (which actually has derived class) will call all the methods on derived class, which it overrides from the base class.
In this case display is overridden so runs fine, but since UniqueDisplay is not, it gives a compile error.
I am not sure whether I am right or wrong about the explanation.
can someone validate?
 
shree vijay
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I guess we have to assume it as you have said -- but i am still unclear that when they say ' Java can actually remember the type of the object assigned to the reference and that is how it implements polymorphism' why can't it 'remember' uniqueDisplay()?
Thanks,
Shree
 
Abhijit Parwatkar
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I think...
U are bound to get compiler error at the call
b.uniqueDisplay();
The error is
The method uniqueDisplay invoked for type Base is not defined.
Compiler is still seeing b of type Base even though d is assigned to it. Base has not defined method uniqueDisplay. So the error.
What happens at runtime though, is object d gets assigned to b. Hence call to b.display actually calls display d.
The behavious seems OK to me.
Correct me if I am wrong.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At compile time all objects are checked to see if that object supports a particular method. So base.uniqueDisplay() will generate a compile error even if base is actually an object of type Derived.
At execution, polymorphism kicks in and the run time actually checks to see what the object is before invoking the method. So if base.display() is run but base is actually an object of type Derived, it will run the display() method of Derived not Base.
Imagine trying to use polymorphism if this wasn't the case!
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use a Base reference to call a Derived method. nor should you be able to.
The compiler uses references to verify the correctness of each call. Derived subclasses Base, so the compiler can verify that all Derived objects know how to display(). The compiler knows display() can be called, so it is satisfied; the run-time, however, only cares about what's in memory, so it invokes the Derived object's display().
On the other hand, the compiler cannot vouch for Base's ability to call uniqueDisplay(). The compiler can't know that all subclasses of Base will have uniqueDisplay(), and it does not allow use of a Base reference to affirm that assumption.
In short, the reference type may only call methods it knows about, the version of the method that executes depends on the runtime object (this is "dynamic binding"). But that same principle does not apply up the class hierarchy.
-----------------
Michael Ernest, co-author of:
 
shree vijay
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, now i am clear
Rgds,
Shree
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic