Polymorphism and abstraction
Polymorphism can be said to be the essence of object-oriented programming. In Java, polymorphism has two different ways of understanding, one is that different methods of objects can use the same method name, both overloaded concepts. The other is that the same object performs the corresponding behavior according to different messages. According to these two methods, it can be divided into static polymorphism and dynamic polymorphism.
Static polymorphism refers to the program at compile time, the system can decide which method to call, so also called compile-time polymorphism
Dynamic polymorphism can dynamically determine the object that the method refers to, also known as run-time polymorphism, only in the running system. The implementation of Polymorphic polymorphism is to override a member method of the same name in the parent class. Dynamic polymorphism as long as the polymorphic is implemented by dynamic binding and rewriting
Three Technologies for polymorphism
- Up-conversion technology, a reference variable for a parent class can point to a different subclass object, or a child object can be treated as a parent class object
- instanceof keyword: The instanceof keyword is used to determine the true type of a run-time object
- Dynamic binding technology: The runtime executes the appropriate subclass method based on the actual type of the object that the parent refers to the variable, thus enabling polymorphism
Upward transformation and downward transformation
In parent subclass inheritance relationships, it is automatic to convert upward along a subclass to the parent class, whereas a forced type conversion must be used from the parent class to the child class to achieve a downward transformation
Polymorphic parameters
When a form parameter of a method is a reference, an object compatible with that reference can be passed to the method, allowing the method to accept formal parameters of different data types
Heterogeneous collections
The most common application of polymorphism is to create a collection of data that is not of the same type but has a common parent, and a collection of different objects is a heterogeneous collection
Abstract
In inheritance, because some methods are overridden in subclasses, there is no need to implement methods in the parent class, so the class is defined as an abstract class. An abstract class cannot be instantiated, and his function is the same.
In Java, you can declare an abstract class by using the keyword abstract, which can appear anywhere the class life is in front of the class keyword
Abstract Method -If a method is declared as an abstract method, then the class must also be declared abstract-any subclass must override the abstract method, unless the subclass itself is abstract
Java Learning (eight)