This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Assigning subclass obj to superclass ref

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, now i am clear
Rgds,
Shree
reply
    Bookmark Topic Watch Topic
  • New Topic