Two concepts are introduced first.
Incremental development
One of the advantages of inheriting technology is that it supports the incremental development mode. you can introduce new Code (including covering the base-class code) without causing bugs in the existing Code. In fact, this mode can isolate new bugs in the new Code.
Even if a bug occurs, it is known to be in the new code, which is easy to make mistakes.
Remember: inheritance represents the display of a relationship, that is, "This new class is a type of the old class ".
Upward Transformation
"Providing methods for new classes" is not the most important aspect of inheritance technology. The most important aspect is to express the relationship between new classes and basic classes. this relationship can be summarized using the phrase "the new class is a type of the existing class.
Because inheritance means that all methods in the base class are equally valid in the export class, all messages that can be sent to the base class can also be sent to the export class.
Since the upward transition is from a more dedicated type to a more general type, it is always safe. that is to say, the export class is a superset of the base class. it may contain more methods than the base class, but it has at least all the methods contained in the base class.
In the process of upward transformation, the only thing that may happen in the class interface is to lose methods rather than get them. this is why the compiler still allows upward transformation without "explicitly indicating transformation" or "without specifying a special mark for transformation.
/***//**
* Title: inheritance & upcasting <br>
* Description: This example explains the concept of "upward transformation" without any functional interfaces. <br>
* Copyright: (c) 2008 thinking in Java <br>
* Company: Augmentum Inc
* @ Author forest he
* @ Version 1.0
*/
Class instrument ...{
Public void play ()... {system. out. println ("upcasting: currently, the base class method is play (). The base class method tune () calls play ()") using the base class object ()");}
Public static void tune (instrument I)... {I. Play ();}
}
Class wind extends instrument ...{
Public static void main (string [] ARGs )...{
Wind flute = new wind ();
Instrument. Tune (flute );
}
}
/** // * In the preceding example, the tune () method accepts the instrument reference. However, in wind. Main (), the tune () method is called by referencing a wind.
* Given the strict type check in Java, it is strange to accept a method of a certain type and accept another type unless you realize that the wind object is also an instrument object.
* In tune (), the program code can work for instruments and all their export classes. This kind of action to convert wind references to instrument references is called "upward transformation ".
*/
Choose between composition and inheritance
Both combination and inheritance allow sub-objects to be placed in the new class. The combination is explicitly implemented, while inheritance is implicitly implemented.
The combination technology is usually used to use the functions of the existing class in the new class, rather than its interfaces. but what users of the new class want to see is the interface defined for the new class, rather than the interface of the embedded object. to achieve this effect, you need to set the existing class objects embedded in the new class to private.
"Is-a" (a) is expressed by inheritance, while "has-a" (a) is expressed by combination.
In OO programming, the most likely method to generate and use program code is to directly package data and methods into a class and use the class objects. you can also use the combination technology to use existing classes to develop new classes. The Inheritance technology is rarely used.
The use of inheritance technology should be used with caution. The use of this technology is only applicable to situations where you are sure to use it effectively. whether to use composition or inheritance, the clearest way to judge is to ask whether you need to transform the new class to the base class. if you need upcasting, the inheritance is necessary. If you do not need it, consider whether you need to inherit it.