Inheritance is the most significant feature of object-oriented. Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities.
First, the basic syntax of inheritance:
In the Java language, using the extends keyword means that one class inherits another. For example:
public class Sub extends base{... }
What exactly does the sub class inherit from the base class? It is divided into the following two situations:
① when the sub class and the base class are in the same package: The Sub class inherits the member variables and member methods of the public, protected, and default access levels in the base class
② when the sub class and the base class are in different packages: the Sub class inherits the member variables and member methods of the public and protected access levels in the base class
Note:
The Java language does not support multiple inheritance, meaning that a class can inherit only one class directly, for example, the following code causes a compilation error:
Class Sub extends Base1,base2 (...)
All Java classes inherit the Java.lang.Object class, either directly or indirectly. The object class is the ancestor of all Java classes.
Second, method overloading (overload)
For a method of a class (including a method inherited from a parent class), if the method name of the two methods is the same, but the parameters are inconsistent, then one method is the overloaded method of the other method
Note:
The overloaded method must meet the following conditions:
① method name is the same
② method has at least one of the parameter types, number, and order
The return type of the ③ method can be different
The modifiers of the ④ method can be different
Iii. method Coverage (override)
The override method must satisfy a variety of constraints:
The name, parameter signature, and return type of the ① subclass method must match the name of the parent class method, the parameter signature, and the return type
The ② subclass method cannot narrow the access rights of the parent class method
The ③ subclass method cannot throw more exceptions than the parent class method
④ method overrides exist only between subclasses and parent classes, methods in the same class can only be overloaded and cannot be overwritten
Static methods of the ⑤ parent class cannot be overridden by a quilt class as a non-static method
The ⑥ subclass can define a static method with the same name as a static method of the parent class to hide the static method of the parent class in the child class
⑦ a non-static method of a parent class cannot overwrite a quilt class as a static method
⑧ private methods of the parent class cannot be overridden by a quilt class
⑨ the abstract method of the parent class can be overridden in two ways: first, the subclass implements the abstract method of the parent class, and the second is the abstract method of the subclass to re-declare the parent class.
⑩ a non-abstract method of the parent class can be overridden as an abstract method
The similarities and differences of method overlay and method overloading
Same point:
Same requirement method
Can be used between abstract and non-abstract methods
Different points:
Method overrides require parameter signatures to be consistent, and method overloading instead
Method overrides require that return types must be consistent, and method overloads do not limit
Method overrides methods that can only be used by subclasses to override the parent class, and method overloads are used for all methods of the same class
Method overrides special requirements for access to methods and exceptions thrown, and method overloading is unlimited
A method of a parent class can be overridden only once by a class, and a method may be overloaded multiple times in the class in which it resides
V Super keyword
Both the super and the This keyword can be used to override the default scope of the Java language, making the masked method or variable visible.
Note:
In the program, the Super keyword is used in the following situations:
① in the constructor method of a class, the constructor of the class's parent class is called through the super statement
② to access masked methods and properties of a parent class in a subclass
Vi. Advantages and disadvantages of succession and principles of use
Advantages and Disadvantages
The biggest weakness of inheritance: breaking packages
Vii. Comparative combination and inheritance
1. Use the inheritance relationship
The parent Class C is abstracted from Class A and Class B, which contains the method1 () and Method2 (), Method3 () methods. Because the Method2 () method is accessed in both Class A and Class B. So declare the METHOD2 () method as the protected type
2. Use the combination relationship
Java Inheritance Features:
(1) The inheritance relationship is transitive. If Class C Inherits Class B and Class B inherits Class A (multiple inheritance), then Class C has properties and methods inherited from Class B, as well as properties and methods inherited from Class A, and can have its own newly defined properties and methods. Inherited properties and methods, although implicit, are still properties and methods of Class C. Inheritance is the most effective means of constructing, building and expanding new classes on the basis of some more general classes.
(2) Inheritance simplifies people's understanding and description of things, and can clearly reflect the hierarchical relationship among related classes.
(3) Inheritance provides the software reuse function. If Class B inherits from Class A, then building Class B requires only a few more features (data members and member methods) that are different from the base class (Class A). This method can reduce the redundancy of code and data, and greatly increase the reusability of the program.
(4) Inheritance by enhancing the consistency to reduce the interface between the modules and interface, greatly increasing the ease of maintenance of the program.
(5) Provide multiple inheritance mechanisms. Theoretically, a class can be a special class of more than one generic class, and it can inherit properties and methods from multiple generic classes, which is multiple inheritance. For security and reliability reasons, Java supports single-inheritance only, and multiple inheritance by using an interface mechanism.
Inheritance (JAVA)