Polymorphism and abstraction are two important concepts derived from inheritance, polymorphism is allowed to exist in many forms, and abstractions allow the creation of abstract classes.
In Java, polymorphism has two ways of understanding: the first is that different methods of an object can use the same method name, which is the concept of overloading. The other is that the same object performs the corresponding behavior according to different messages, or it can be thought that sending a message to an object allows the object to choose the appropriate behavior for itself. Depending on the two approaches, polymorphism is divided into static polymorphic and dynamic polymorphism:
Static polymorphism refers to the program at compile time, the system can decide which method to call, so also known as compile-time polymorphism. In Java, a static polymorphic implementation is a method overload whose invocation rules call the overloaded methods in the corresponding class accordingly, depending on the type of the object at the time of definition.
Dynamic polymorphism refers to the operation of the system in order to dynamically determine the object referred to by the method, so also known as run-time polymorphism. Dynamic polymorphism is implemented by overriding a member method of the same name in the parent class whose invocation rules are called according to the type of the object when it is not defined at the time of instantiation, and corresponding to the member method of the same name in the corresponding class. In other words, dynamic polymorphism is mainly implemented by dynamic binding and rewriting mechanisms.
In Java, the use of dynamic binding and rewriting mechanisms to achieve polymorphism, first of all to grasp the following three basic technical concepts:
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 type.
instanceof keyword: The instanceof keyword is used to determine the true type of a run-time object.
Dynamic binding technology: The runtime executes the corresponding subclass method according to the actual type of the object referred to by the parent class, thus achieving polymorphism.
Upward transformation and downward transformation
Upward Transformation: automatic conversion from low-precision to high-precision conversiondown transformation: converting from high-precision to low-precision, with castfor reference data typescannot be converted from basic data typesThe reference data type must be an inheritance relationshipa reference to the parent class is directed to the object of the subclass before it can beEg:father fa = new Son ();son son = (son) new Father ();(cannot be passed)Eg:father fa = new Son ();son son = (son) FA;(a reference variable for a parent class can point to a different subclass objector a sub-object can be treated as a parent class type).
In Java, a class can be designed as an abstract class if we don't need the instantiation of the class. The so-called abstract class is a class that cannot be instantiated. In an abstract class, all of the other functions of a class exist, and member variables, methods, constructors can all be accessed in the same way, and we simply cannot create an instance of an abstract class. Use the keyword abstract to declare an abstract class that can appear anywhere in front of the class keyword at the time it is declared.
Declaring a method as an abstract method has two results: the ① class must also be declared as an abstract class. If a class contains an abstract method, the class must also be an abstract class, and any subclass of ② must override the abstract method, unless the subclass itself is abstract.
There is no method body for an abstract method, and a class that contains an abstract method must also be declared as an abstract class. The abstract method must be overridden in all subclasses of the abstract class.
java--Polymorphism and abstraction