Inherited
Single inheritance/Multilayer inheritance--super keywords
Instance Application
This/super Use comparison
Replication of the method "relationship between the overload and the"
Construction method Invocation order of child parent class
Use of the parent class private variable
Final keyword
1: Inheritance
(1) extracts the same properties and behavior from multiple classes, encapsulates it into a class, and
then creates a new class without having to start over, inheriting the class that you just defined.
(2) Benefits:
A: Improve the reusability of the code.
B: Having a relationship between classes and classes is a precondition for polymorphism.
(3) When do I use inheritance?
A: If there is a relationship between the classes:
is a, you can consider using inheritance. When there is a relationship between classes
B: Do not use inheritance in order to inherit some of the functionality.
(4) Features of inheritance:
A:java only supports single inheritance and does not support multiple inheritance.
Why? If multiple inheritance is supported, there will be a problem with ambiguous calls. The
B:java supports multilayer (heavy) inheritance.
(5) What is the difference between super and this? The
A:super is a keyword that represents the storage space identity of the parent class. (Can be understood as a reference to a father)
B: It is similar to the use of this
A: member variable
this. Variable--This class of
Super. Variable--The parent class
B: Constructor method
This (...) .) --This class of
super (...) --The
C: Member method of the parent class
this. Method name ()--the
Super. Method Name ()--the use of member variables in the parent class
(6) Child parent class:
A: The name is different, this is too simple. The corresponding call to
B: The same name, when the subclass object is in use:
First find the subclass local scope
and then find the subclass member Scope
finally find the parent class member scope
(7) The use of member methods in the child parent class:
A: Name does not It's too easy. The corresponding call to
B: The same name, when the subclass object is in use:
First find the subclass of
and then look for the parent class
C: Method overrides
in the subclass, the method declaration (modifier, return value, method name, parameter list) the same situation.
Precautions:
A: Private methods in the parent class cannot be overridden
B: The subclass method must have access rights greater than or equal to the access rights of the parent class
C: Static can only override static. (This cannot be counted because the static is related to the class)
(8) Use of the constructor method in the child parent class:
A: During the initialization of a subclass, the initialization of the parent class is performed first.
Because the child class is constructed by default, there is a super ().
Why? Subclasses to use the member variables of the parent class, this initialization must be done before the subclass is initialized.
Therefore, the initialization of the parent class is performed first during the initialization of the subclass.
B: If the parent class does not have a parameterless construction method
A: Use super to invoke the parameter constructs of the parent class. Recommended way.
B: Use this to invoke other constructs of itself.
2: code block
(1) Order of execution:
Static code blocks---Construction code block--construction method
Example: A static code block runs only once in a program load, and static code blocks take precedence over the construction method execution.
(2) Precautions:
Static code blocks execute only once ". class files are loaded once"
3:final
(1) is a keyword that can be used to decorate classes, member variables, and member methods.
(2) Features:
The class it modifies cannot be inherited.
The member variable that it modifies is a constant.
It modifies a member method that cannot be overridden by a quilt class.
Java Basics-Day Nineth