Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Aditi Msc   on May 19 In Java Category.

  
Question Answered By: Cheri Garcia   on May 19

As far as I know, you can't do that. When extending, you inheirit both
the implementation and the type. When you cast  the derived  class to
the base, you are using the type of the base  class. In a sense, this
is a contract that the derived class  must implement. Since the
relationship is extends, the type is implemented by inheiriting the
base class implementation. So, if the derived class did not override
the base clase toString(), then the base class implementation would be
used. Since you override the toString() in the derived class, this is
the toString() that gets invoked. In other words, you instantited a
derived class. That object  is a derived object that also implements
the type of the base class. But the object is still of type derived.

On a side note, I believe what you are asking can be implemented in
C++ or C# among other languages I assume.

Hope that helps and I may be off so hopefully others will correct me
where I was wrong.

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on How do we cast a Derived class to Base class Or get search suggestion and latest updates.


Tagged: