The magic lies in ==== "inheritance, in inheritance
1. Inheritance
1. Role: inheritance is a powerful means to reuse program code.
2. Understanding inheritance: when multiple classes have the same attributes, we can extract a parent class from these classes. When the Child class inherits the parent class, subclass can use all methods and attributes of the parent class. (Subclass does not need to define the same attributes as the parent class)
Ii. Basic inheritance syntax
1. in java, the keyword Extends is used to indicate that one class inherits another class.
Resolution: The code above indicates that the Sub class inherits the Base class. So what does this class inherit? Analysis is performed in two cases.
1) when the Sub class and Base class are in the same package:
The Sub class inherits the Public, Protected, and default access-level member variables and member methods in the Base class.
2) When the Sub class and Base class are in different packages:
The Sub class inherits the Public and protected access-level member variables and member methods in the Base class.
For ease of description, the general method "subclass inherits attributes and methods of the parent class" is sometimes used.
3) For example, assuming that the Sub and Base classes are in the same package, the following program demonstrates which member variables and methods of the Base class can be inherited in the Sub class.
Note:
1) the Java language does not support multi-inheritance. A class can only inherit one class directly.
The error code is as follows:
Class Sub extends Base1, Base2, Base3 {.......}
1) Although a class can only have one direct parent class, it can have multiple indirect parent classes.
The Code is as follows:
Class base1 extends base2 {.....}
Class Sub extends Base1 {.....}
Iii. Method Overloading
1. conditions that must be met by the overload method
1) Same method name
2. There must be at least one difference in the parameter type, number, and order of the method.
3) the return value types of methods can be different.
4) The modifier of the method can be different.
2Eg
Iv. Method coverage
(1) The coverage method must meet multiple constraints.
1) The name, parameter signature, and return type of the subclass method must be the same as that of the parent method, parameter signature, and return type.
2) subclass methods cannot reduce the access permission of the parent method.
3) The subclass method cannot throw more exceptions than the parent method.
4) method override only exists between subclass and parent class (including direct parent class and indirect parent class. In the same type, methods can only be overloaded and cannot be overwritten.
5) Static Methods of the parent class cannot overwrite non-static methods of the quilt class.
6) subclass can define a static method with the same name as the static method of the parent class, so that the static method of the parent class can be hidden in the subclass.
7) the non-static method of the parent class cannot overwrite the static method of the quilt class.
8) the private method of the parent class cannot be overwritten by the quilt class.
5. super keywords