1, the role of inheritance? Benefits? Harm?
Inheritance: Implements code reuse through inheritance. All classes in Java are obtained directly or indirectly through the Java.lang.Object class. Inherited classes are called subclasses, and inherited classes are called parent classes. Subclasses cannot inherit member variables and methods in the parent class that have access rights private. Subclasses can override the parent class's methods and name member variables with the same name as the parent class. However, Java does not support multiple inheritance, which is the ability of a class to derive from multiple classes of superclass.
Advantages: A because most of them are inherited, implement code reuse, reduce the amount of code written;
b It's easy to modify and extend existing implementations
Cons: A breaks the encapsulation because the base class exposes implementation details to subclasses
B white box reuse, because the inner details of the base class are usually visible to the child class
c when the implementation of the parent class changes, it is possible to change the corresponding subclass
D cannot change the implementation inherited by the parent class at run time
2, the advantages of the interface? Harm?
Pros: Helps the Java language implement a feature similar to multi-inheritance. But the implementation of multi-inheritance does not cause a network relationship between the classes in the code, but rather a clear tree-like feeling, similar to a family tree.
Cons: If you add a new method to a Java interface, all classes that implement the interface have to write a concrete implementation.
3. What is the role of polymorphism? Benefits? Harm?
Function: Simple is an interface, a variety of implementations, the performance of inheritance is polymorphic (without inheritance there is no polymorphism.) )
A application does not have to write a function call for each derived class, only the abstract base class needs to be processed. Greatly improve the reusability of the program.
The functionality of a derived class of B can be called by a method of a base class or a reference variable, which is called backward compatibility and can improve extensibility and maintainability.
Advantages: A replaceable (can replace one existing code);
b Extensibility (adding new subclasses does not affect the properties of the original Class);
C interface;
D flexibility;
E Simplification
Cons: A "cloak" private method. Only non-private methods can be shrouded, although the compiler will not error, but will not follow what we desire to implement. In the export class, for the private method in the base class, a different name is adopted.
b Domain in the transformation of the problem. For member variables (fields), the derived class will occupy the member variable inherited from the base class and its own member variable (as well as the name of the variable), and the different storage spaces will be allocated, so that the exported class will have two nominal domains. In order to obtain the domain of the base class, it is necessary to actually visit the famous Super.field capability, while the tacit domain is the domain of the export class.
C static tricks are related to a class rather than to an object, so it does not have polymorphic actions.
Inheriting polymorphic interfaces