1. Relationship of construction methods in inheritance
1. All constructor methods in a subclass access the constructor of the parent's hollow parameter by default, unless the parent class is called explicitly using Super/this or other constructor methods of this class.
2. Calls to this class or to the parent class construction method in a class can only be constructed in a constructor method and cannot be called in an instance method (more cannot be called in a class method) because:
- When the instance method is called, the instance object has been created, and it is no longer possible to use This/super to initialize this instance or the parent class instance
- The class method is loaded when the class is loaded, and the instance object has not been created yet, it is not possible to use this or super
2.
Because subclasses inherit data from the parent class (member variables), data from the parent class may also be used.
Therefore, before subclasses are initialized, it is important to complete the initialization of the parent class data first.
In other words, the creation of an object means that all its parent classes will be created.
3. The first statement of the subclass construction method:
If this is the (...) Indicates that another constructor for this class is being called, and you can continue to use this (...) in another constructor method. Call this class of other construction methods, if there are multiple construction methods, you can continue to call, but not recursive call, there will always be a construction method, the first statement is not this ().
At this point, you can explicitly call the constructor of the parent class super (...); Or write nothing, the system defaults to calling the parent class's NULL parameter construction method.
In summary, when a subclass object is created, the constructor of the parent class is always called, until the topmost object of object is created, and the sub-class object under it is created.
4. Examine the parent class, the execution order of the subclass code block
From this case, you can see the process of initializing an instance of a class with inheritance, and why you cannot use this or the Super keyword in a class method
5. What if there is no null parameter construction method in the parent class?
1. Subclasses can explicitly invoke the other constructor of the parent class by Super
2. Subclasses can invoke the other constructor methods of this class through this, but other constructs of this class must first use Super (...). The parameter structure of the parent class was accessed (because the parent class has no null parameter constructs)
6. Summary
If the parent class does not have an empty parameter construct, the subclass's constructor must explicitly call the parent class with the parameter construct super (...);
Be sure to note:
Super (...) Or this (...) Must appear on the first statement of the constructor method
Otherwise, there will be multiple initialization of the parent data
7. What happens to the constructor of a subclass if the parent class does not have a parameterless constructor method?
Error.
How to solve it?
A: Add a non-parametric construction method to the parent class
B: Call the other parameters of the parent class by using the Super keyword to construct the method
C: Subclasses use this to invoke the other constructor methods of this class
The subclass must have a constructor that accesses the parent class, otherwise the parent data is not initialized.
Precautions:
This (...) or Super (...) Must appear on the first statement.
If it is not placed on the first statement, it is possible to initialize the data of the parent class more than once, so it must be placed on the first statement.
8. Relationship of member methods in inheritance
A member method with the same name and different name exists in the child parent class
Conclusion :
To access an instance method through a subclass object
First Look in the subclass (whether the subclass is overridden, or is a subclass-specific method)
Then look in the parent class (subclass not rewritten, but inherited from parent class)
9. Relationship of member methods in inheritance:
A: The method in the subclass is not the same as the method declaration in the parent class, this is too simple.
B: The method in the subclass is the same as the method declaration in the parent class.
To invoke a method from a subclass object:
A: First look for the sub-class, see if there is this method, there is the use of
B: Again look at the parent class, there is no this method, there is the use of
C: If there is no error.
10. Overview of Method overrides
Method Overrides overview
- A method declaration that is identical to the parent class appears in the child class
Called method overrides override/rewrite overwrite
Usage Features:
- If the method name is different, the corresponding method is called
- If the method name is the same, eventually using the subclass's own (when using a reference to the subclass, in polymorphic cases, using the parent class's reference, it is possible to call a static method of the parent class)
Methods to override the application:
- When a subclass needs the functionality of a parent class, and the feature body subclass has its own content, you can override the methods in the parent class, which is to follow the functionality of the parent class and define the content that is unique to the subclass.
- The override of a method is a condition of a polymorphic implementation
11. Considerations for Method overrides
- Private methods in parent class cannot be overridden, compile error
- The subclass overrides the parent class method, the access permission cannot be lower (later), otherwise the compilation error
- Subclasses override the parent class method, return a value type that can be the same, or be a subtype of the parent class return value type
- Instance methods of the parent class (non-static methods), subclasses cannot be redefined as static methods
- Parent class static Methods (class methods), subclasses must also be "rewritten" through static methods (although the compilation and use are not error, but in fact, this is not the method of rewriting, but the phenomenon is true, as to why the method is not rewritten, polymorphic will be explained)
- Subclasses override the parent class method, according to the overriding principle (access permissions cannot be smaller, return a value of the same or subclass, the method name is the same, the formal parameter list is the same); otherwise, the same name method defined in the subclass is an overload of the method (inherited methods and subclass-defined methods make up the overloads) A subclass method cannot be overloaded if it differs from the return value of a method inherited from a parent class only
- Subclass if you want to override the method of the parent class, it's best to have the signature of the method identical
(An important use of an overriding method is that a reference to a parent class can point to a method of a subclass, but the "override" of a static method is still called the method of the parent class in polymorphism, so, from this point of view, the subclass's overriding of the static method of the parent class is not a true method of rewriting)
12. Considerations for Method overrides
A: Private methods in the parent class cannot be overridden
Because the parent class private method subclass simply cannot inherit
B: When subclasses override parent class methods, access permissions cannot be lower, preferably consistent
C: Parent class static methods, subclasses must also be overridden by static methods
In fact, this is not the method of rewriting, but the phenomenon is true, as to why not the method of rewriting, Polymorphic explained
When a subclass overrides a parent class method, it is best to declare exactly the same.
Otherwise the method with the same name is overloaded, you must make the formal parameter list different
Big Data Java Foundation 11th Day