File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assigning subclass obj to superclass ref 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 "Assigning subclass obj to superclass ref" Watch "Assigning subclass obj to superclass ref" New topic

Assigning subclass obj to superclass ref

shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
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?

Milind Deodhar
Ranch Hand

Joined: Dec 06, 2000
Posts: 32
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

Joined: Sep 18, 2000
Posts: 208
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()?
Abhijit Parwatkar

Joined: Dec 07, 2000
Posts: 10
Here is what I think...
U are bound to get compiler error at the call
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

Joined: May 05, 2000
Posts: 13974
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!

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

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:

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Thanks a lot, now i am clear
I agree. Here's the link:
subject: Assigning subclass obj to superclass ref
It's not a secret anymore!