inherit
Inheritance is an indispensable design idea in object-oriented programming, is the foundation for code reusability, and the main way to improve code scalability.
Is an is-a relationship xxx is xxxx cats are animals, dogs are animals
Inheritance is to derive a new class from an existing class. The new class can absorb the attributes and behaviors of the existing class and can extend new capabilities.
-Use extends keyword in Java to indicate inheritance relationship.
-Java does not support multiple inheritance. Single inheritance makes Java's inheritance relationship very simple. A class can only have one direct parent class.
-After inheritance, the child class can call all the non-private properties and non-private methods of the parent class.
Terminology
Dog class is a subclass of Animal class, Animal class is the parent class of
Dog class Dog class is a derived class of Animal class, Animal class is the base class of Dog class
Transitivity of inheritance
Barron class inheritance-"dog class inheritance-"animal class inheritance-" object
When one does not inherit any class, jvm will let the class inherit the Object class by default
Object is the base class provided by java for all classes
Construction method in inheritance
The subclass constructor always calls the parent constructor first. By default, the parent constructor is called without parameters.
You can use the keyword super to call any construction method of the parent class in the first line of the subclass construction method.
If you use super, you must write it in the first sentence of the construction method.
Use of super keyword
The super keyword represents the reference of the parent class, the main purpose in the program:
To call the construction method of the parent class in the subclass construction method, please note: the super statement can only appear in the first line of the subclass construction method body
Use "super. member variable name" to refer to the parent class member variable
Use "super. method name (parameter list)" to access the method of the parent class
The difference between this, this usually refers to the current object, super usually refers to the parent class
Method rewriting (OverRide)
When the method implementation of the parent class cannot meet the needs of the child class, the function method defined in the parent class can be overwritten.
Method rewriting rules
Same method name, same parameter list
The return value type is the same
Same access rights
That is exactly the same as the parent method structure