Fig. 4 Pure Substitution
That is, the base class can receive any message you can send to the derived class because the two have exactly the same interface. All you need to do is upcast from the derived class and never look back to see what exact type of object you’re dealing with. Everything is handled through polymorphism.
When you see it this way, it seems like a pure is-a relationship is the only sensible way to do things, and any other design indicates muddled thinking and is by definition broken. This too is a trap. As soon as you start thinking this way, you’ll turn around and discover that extending the interface (which, unfortunately, the keyword extends seems to encourage) is the perfect solution to a particular problem. This could be termed an “is-like-a” relationship, because the derived class is like the base class—it has the same fundamental interface—but it has other features that require additional methods to implement:
Fig. 5 Implement Interface
While this is also a useful and sensible approach (depending on the situation), it has a drawback. The extended part of the interface in the derived class is not available from the base class, so once you upcast, you can’t call the new methods:
Fig. 6 Deliver Message
If you’re not upcasting in this case, it won’t bother you, but often you’ll get into a situation in which you need to rediscover the exact type of the object so you can access the extended methods of that type. The following section shows how this is done.
B.原文的翻译
多形性对于一个面向对象的语言来说,是继数据的抽象化和继承之后最为重要的特点。论文网
它提供了另一种方式去分离本来用户想用与继承的接口,而且它让从什么到怎么的过程中更加的低耦合,多形性允许改善代码的组织性和稳定性,就好像让一个可扩展的程序变得拥有了成长性,而且这种的成长性的产生不仅仅使用与程序的开头,当新的特征出现的之后它也是可以实现这种功能的。 Java编程思想英文文献和中文翻译(5):http://www.751com.cn/fanyi/lunwen_30180.html