In the previous chapter, we carried out object-oriented learning,
found that if the properties and methods in several of the defined classes are duplicated,
Does the code look very redundant? , is there a thought that can be simplified?
Yes! Now, let's look at inheritance to solve this problem.
1. Inheritance-Overview
Inherited:
1, improved Code reusability
2, the relationship between classes and classes, that is, the characteristics of polymorphism
Note: Do not inherit from simplified code in order to get the functionality of other classes
You must have an owning relationship between the class and the class to inherit.
In the Java language: Java only supports single inheritance and does not support multiple inheritance
Because multiple inheritance is a security risk: the same functionality is defined in multiple parent classes
And the function content is not the same, the subclass object is not sure which one to run.
But Java retains this mechanism and uses a different form of representation to do it: multiple implementations.
Java supports multiple layers of inheritance, i.e. an inheritance system
How do you use the functionality of an integrated system?
To use the system, first look at the description of the parent class of the system, as the parent class defines the system's CPC functionality
By understanding the common features, you can know the basic functions of the system
Then the system can be used basically.
So when you make a specific call, you create the object of the most child class, why?
One is because it is possible that the parent class cannot create an object.
The second is to create subclass objects that can use more features, including basic and special
Simple sentence: Look up the parent class function, create subclass object using function
2. Characteristics of inheritance
After the child parent class appears, the class member features:
Members in class:
1, variable.
2, function.
3, constructor.
1, variable
If a non-private member variable with the same name appears in the child class is
Subclass to access the variables in this class, use this
Subclass to access a variable of the same name in the parent class, use super
2, function
When a subclass appears with the same function as the parent class,
When a subclass object calls the function, the contents of the subclass function are run.
This condition is another feature of the function: override (Overwrite)
Overrides can be easily extended
Covered:
1, the subclass overrides the parent class, must guarantee that the subclass permission is greater than or equal to the parent class permission, can overwrite, otherwise the compilation fails
2, static override static
Remember everyone:
Overloading: See only the parameter list of functions with the same name
overriding: The Child parent class method is identical
3, constructor in child parent class
When the child class object is initialized, the constructor of the parent class is also run,
That's because the constructor of the subclass defaults to the first line with an implicit statement super ()
Super (): a constructor that accesses the empty arguments of the parent class, and all constructors in the subclass default to the first row are super ();
Super () refers to Fu () {}
Why subclasses must access constructors in the parent class
Because the data subclasses in the parent class can be obtained directly, the subclass object is created,
You first need to see how the parent class initializes the data.
So when the subclass initializes the object, it accesses the constructor in the parent class first.
If you want to access a constructor that is specified in the parent class, you can specify it by manually defining the Super statement
NOTE: The super statement must be defined in the first row of the subclass constructor, because the initialization action should be done first
There is either this () or super () in the constructor, only one. (Object is the parent class of all classes)
Conclusion:
All constructors of a subclass, by default, access the constructor of the parent's hollow argument.
Because the first line within each constructor of a subclass has an implicit super ();
When the constructor of a parent class does not have an empty argument, the child class must manually pass the Super statement
In the form of a constructor that specifies access to the parent class.
Of course: The first row of a subclass's constructor can also manually specify the this statement in the text class
's constructor. At least one constructor in the subclass accesses the constructor in the parent class.
3. Final keyword
Final: Finally. As a modifier.
1, you can modify the class, function, variable.
2, a final modified class cannot be inherited. The drawbacks of inheritance (breaking the encapsulation).
In order to avoid being inherited, quilt-like replication function.
3, the final modification of the method can not be replicated.
4, the final modified variable is a constant, can only be assigned once,
That is, you can modify a member variable, or you can modify a local variable.
When the value of some data is fixed when describing things, then this is for
Enhanced reading, giving these values a name. Easy to read. And this
The value does not need to be changed, so the final decoration is added. As constants: The Writing of constants
Specification All letters are capitalized if they consist of multiple words. Between the words through _ connection.
Modifier: Lock variable block
5, the inner class can only access the final decorated local variables.
Different Java Learning (v) Inheritance on