When learning inheritance, the most obvious way to create an inheritance hierarchy is to take a "pure" approach. That is, only methods that have been established in the underlying class or interface can be overwritten in the derived class, as shown in the following illustration:
It can be described as a pure "belong" relationship, because the interface of a class has defined what it "is" or "belongs to". By inheriting, you can guarantee that all derived classes have only the interfaces of the underlying class. If you follow the diagram above, the derived class will not have anything other than the interface of the underlying class.
It can be imagined as a "pure substitution" because derived class objects can be perfectly replaced for the underlying class. When using them, we do not need to know any additional information about the subclass. As shown below:
That is, the underlying class can receive any messages we send to the derived class because they have exactly the same interface. All we have to do is derive from the form, and never need to go back and check what the exact type of object is. All the details have been perfectly controlled by the form of polymorphism.
If you think about it this way, a pure "belong" relationship seems to be the only sensible way to design, and any other design method can lead to confusing ideas, and there are big problems with definitions. But the idea belongs to another extreme. After careful research, we find that the extended interface is a particularly effective solution for some specific problems. It can be called a "similar" relationship, because the extended derived classes are "similar to" the underlying classes-they have the same underlying interface-but they add some features that require additional methods to implement. As shown below:
Although this is a useful and sensible approach (determined by the specific environment), it has a disadvantage: the part of the derived class that extends to the interface is not available in the underlying class. So once you've traced the styling, you can't call the new method:
If you do not trace at this time, this problem will not occur. In many cases, however, the exact type of object needs to be verified so that it can access that type of extension method. In the following section, we specifically describe how this is achieved.