Briefly describe Java inheritance and polymorphism

JAVA inheritance and polymorphism

1. Inheritance
1. The concept of
inheritance. Inheritance is that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance domain and methods of the parent class, or the subclass inherits methods from the parent class, so that the subclass has a parent The same behavior.
Classes derived from inheritance are called subclasses or derived classes; classes that
are inherited are called parent classes, base classes, or superclasses.
2. Why use inheritance
? It can improve the reusability of the code.
②, reduce the redundancy of the code, make the code more concise.
③ Improve maintainability.
3. Types of inheritance It
should be noted that Java cannot be multiple inheritance.
It can be single inheritance, multiple inheritance, different classes inherit the same class
4, the characteristics of inheritance The
subclass has the non-private properties and methods of the parent class.
The subclass can have its own attributes and methods, that is, the subclass can extend the parent class.
Subclasses can implement the methods of the parent class in their own way.
5. Inheritance keyword
①, the keyword inherited in Java is extends.
We can extract the public attributes and behaviors and place them in the parent class, and the subclass can extend the parent class to achieve more features. This saves a lot of code, the program becomes more concise and easy to expand.
②, super keyword The
super keyword refers to the attributes and methods in the immediate parent class of the current object.
In addition to calling the method of the same name in the parent class, super can also be used in the construction method,
(follow the principle: super should be placed in the first line; the construction method directed by super must be in the parent class.)
③, final keyword
The final keyword declares that a class can be defined as a class that cannot be inherited, that is, the final class; used to modify a method, which cannot be overridden by subclasses; used to modify a variable, which becomes a constant; used to modify an attribute, the Attributes cannot be assigned; used to modify objects, which cannot be referenced.
Second, the coverage of the method
1. For example, ordinary cars, trucks and cars, their operation is the same, but according to the truth, the operation of trucks and cars should have their own independent methods, and should not be the same as ordinary cars. be consistent.
The subclass needs to improve the operation method of the parent class and become the subclass's own operation method. This requires rewriting the operation method in the subclass to override the operation method of the parent class. This approach is called method coverage in JAVA. . (Also called method rewriting)
2. The difference between rewriting and overloading
Rewriting can only occur in subclasses;
overloading can occur in any class;
third, polymorphism
1, the same object, calling different classes Examples to get different results. This is called polymorphism.
2. The object created by the parent class reference can only call the methods inherited by the subclass from the parent class (including the overridden methods of course), and cannot call the methods extended by itself. Just like we say: Leopards are animals (upward transformation), we can also say: Leopards can run (call methods inherited from the parent class), but we cannot say: animals are leopards (downward transformation), nor can we say: animals can crawl Tree (upcast objects cannot call methods extended by subclass objects).
3. Inheritance is a manifestation of polymorphism.

Guess you like

Origin blog.csdn.net/tan1024/article/details/110089073