This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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?
Joined: Sep 18, 2000
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
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.
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!
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: