What is inheritance
By stripping out some of the common things in a class and forming a new class, each other class retains its own unique feature, and inherits the new class with the keyword extends, which can eventually achieve all kinds of primitive same effects, but in each class, a "extends new class" is used. You can reduce the amount of code in the new class that is almost the same magnitude.
The inherited format
class extends new class {}
The new class is a class that is stripped out with common attributes.
The applicable scenario for inheritance
In the presence of a "is a" relationship, for example cat is a animal, dog is a animal.
Characteristics of inheritance
- Classes in Java only support single inheritance
- Java can be multi-layered inheritance, that is, multiple inheritance system, a extends B, B extends C, then A is essentially extends b,c
- Subclasses cannot inherit private members and methods of parent class
- Subclasses cannot inherit the constructor of a parent class, but can access it through super.
Benefits of Inheritance
- Improved reusability of the code
- Simplifies the maintenance of your code
- Is the basis of tectonic polymorphism
The disadvantage of inheritance
- The idea of not conforming to the low-coupling high cohesion makes the relationship between class and class more close, and the relation between class and class is complex
- Breaking the encapsulation
The benefit of encapsulation is to access private variables only from methods exposed by the class, but inheritance can indirectly access the private members of the parent class from the subclass method through the inherited parent class method.
In addition, if you want subclasses to inherit attributes (member variables) that are common, you cannot encapsulate them because private member variables and methods cannot be inherited, which constitutes a conflict.
Member Relationships in inheritance Member variables
The name of the member variable in the subclass is different from the name of the member variable in the parent class and can be distinguished directly from the name;
The member variable name of the subclass is the same as the member variable name in the parent class, which is distinguished by this and super. The member variable is a subclass, and the member variable of super is the parent class (not private)
In summary, when you access variables in a method of a subclass, if you do not explicitly differentiate between this and super, the order of the lookups is as follows:
A. Find the local scope of the subclass method and use it.
B. In the subclass of the member scope to find, there is the use.
C. In the member scope of the parent class, use it.
D. can not find, the error.
Construction method
The constructor method of a subclass defaults to accessing the parent class's parameterless constructor, which is for the subclass to access the initialization of the parent class data, if there is no parameterless constructor in the parent class.
1. The subclass is required to explicitly call the parameter construction method through super, otherwise it will be an error.
2. Subclasses call the other constructs of their own through this, but eventually there must be a construct to access the parent class.
3. Let the parent class provide an argument-free construct.
Member Methods
The member method of the subclass is different from the member method name in the parent class, and can be distinguished directly by the name;
The member method of the subclass is the same as the member method name in the parent class, which is distinguished by super and this, such as this.function () or super.function ();
If you do not explicitly distinguish between this and super, the last way to access a method by using a subclass object is to look in the order:
A. Find in subclasses and use them
B. Look in the parent class and use
C. can not find, the error
Method overrides in Inheritance (override)
The subclass and its inherited parent class have exactly the same method, from the return value to the argument list are identical, the modifier is not the same as the strict requirements;
overridden Features:
1. Method overrides require that the access modifier scope of the subclass override method must be greater than or equal to the access modifier scope of the parent class's corresponding method, such as the parent class is public, the subclass cannot be protected.
2. The return value type of the subclass override method must be consistent with the parent class method
3. The parameters of the override method must be consistent with the parent class method
and overloading (overload) are different:
1. Overloading occurs in a class, with the same method name, with different parameter lists, but with no requirement for modifiers and return value types, because different functions cannot be distinguished by modifiers and return value types at invocation.
2. Overrides occur in inheritance, require method name, parameter list, return value type exactly as the modifier access scope is not lower than the parent class. overriding the purpose of overwriting the parent class method, the other requirements can be fully overloaded in the subclass to do.
The Super keyword in inheritance
1. Super represents the parent class object;
2. Access the parent class non-private member variable by super. Variable name to access the parent non-private member method through the Super. Member method;
3. Call the Parent class construction method by Super () or super (parameter), and super () is called by default in the subclass constructor method;
Some summary of "Java Foundation" inheritance