java inheritance and polymorphism

Inheritance: Inheritance and polymorphism are an important part of java. Inheritance is a mechanism to reduce repetitive program code. It abstracts the same instance variables and methods (and constructors) of multiple classes into what is owned by one class. This class It is the parent class of the previous classes. Saying that a bit...
On the other hand, if a class inherits a class, then it inherits the instance variables and methods of that class (permissions allow).
How can we determine who is the parent class and who is its subclass?
In the book, the "is a" method is used, that is: wine is wine, then wine is a subclass of wine, and wine is the parent class; white wine is wine, that is, white wine is a subclass of wine, and the subclass is wine. Subclasses of the class can also pass the "is a" method, ie white wine is also wine.
For example: class A inherits class b; class A extends B; then A will have instance variables and methods (including constructors) of class B.
Subclass A can also override methods inherited from class B, requiring the subclass method to have the same method name and parameters as the parent class method, and the return type must be compatible (that is, the return type is the same or a subclass of that type). When overriding, you cannot reduce the access rights of the method.
There are also method overloading. Overriding is different from overriding. The return type of overloading can be different, the access rights of the method can be changed, and the parameters need to be different. The same as overriding, the method name is the same. (The parameters are different, the return type is the same or not, all are overloading.
The method invocation order in inheritance is to call the method in the subclass first, if the subclass does not have the method, it will be called to its parent class
, if the subclass overrides After calling this method, the overridden method will be called; if you want to call the method of the same name in the parent class after calling the method of the subclass, you must use supper to call, for example: supper.method name();
polymorphism: multiple State is based on inheritance. When using polymorphism, the reference type can be the parent class of the actual object type; that is, the parent type can be used as the parameter and return type of the method or the type of the array. In this case, the extension program When you only need to create a new class that integrates the parent class, you don't need to modify the already written code. For example
class A{
void go(A a){ //The method go in A, the parameter is an instance of A.
System.out.printf(“duo tai”);
} }
class B extends A{ //B will inherit the go method of class A,
}
class F extends B { //F will inherit the method inherited by class B.
}
class C_Test{
public static void main(String[]args){
A d=new B(); //The reference type is class A, and the referenced object is indeed class
B b b=new B();
F f= new F();
d.go(f); , //call the go method with reference to variable d, and the parameter is an instance of class F, then execute the go method
d.go(b) in class F //call with reference to variable d go method, the parameter is an instance of class B, the go method in class B will be executed
} } //So the go method can take any kind of A, as long as the incoming object is a subclass of A, the method will be It can run.
If you need an output different from the parent class A, just override the inherited parent class method in the subclass,
for example: void go(A a){
System.out.printf(“duo tai”); } //The key to overriding or overloading is the parameter (input parameter) and return type.
If the subclass defines an overloaded method of the parent class, the overloaded method will not be called when calling, but the method in the parent class will be called.
When writing the program code, declare the parameters as the parent class type. You can pass in any subclass object at runtime. The result of the operation depends on whether you override the method in the parent class in the subclass. The result in the class overriding method, or the result in the parent class method if there is none.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326597569&siteId=291194637