Polymorphism: determines the actual type of the referenced object during execution rather than during compilation, and calls the corresponding method based on the actual type.
Three necessary conditions for Polymorphism:
1. Inheritance required
2. Rewrite
3. parent class references to subclass objects
Abstract class
When an abstract keyword is used to modify a class, this class is called an abstract class. When abstract is used to modify a method, this method is called an abstract method.
Classes that contain abstract classes and methods must be declared as abstract classes. abstract classes must be inherited and abstract methods must be overwritten.
Abstract classes cannot be instantiated.
Abstract METHODS only need to be declared, rather than implemented
If the subclass does not implement the abstract method of the parent class, you can write the prototype of the abstract method again, or declare the subclass as an abstract class.
Final keywords
The final variable value cannot be changed.
Final member variables
Final local variables (parameters)
The final method cannot be overwritten.
The final class cannot be inherited.
Interface
An interface is a set of definitions of abstract methods and constant values.
Essentially, an interface is a special abstract class that only contains the definition of constants and methods, without the implementation of variables and methods.
The interface is defined as follows:
Public interface Runner {
Public static final int id = 1;
Public void start ();
Public void run ();
}
Multiple irrelevant classes can implement the same interface
A class can implement multiple irrelevant interfaces.
Similar to inheritance, There Is A polymorphism between interfaces and implementation classes.
The attribute declared in the interface is public static final by default; it can only be public static final
Only abstract methods can be defined in the interface. By default, these methods are public and can only be public.
Interfaces can inherit other interfaces and add new attributes and abstract methods.
The keyword of the implementation interface is implements.
An interface can inherit classes, but a class cannot inherit interfaces. A class can only implement interfaces.
Interfaces can be inherited from each other.