first, before introducing polymorphism, introduce an important knowledge point--java type of reference variable
There are two types of reference variables in Java:
1. Compile-time type , which is determined by the type used when declaring the variable;
2, the Run-time type , by the actual assigned to the variable of the object decision;
When the compile-time type is inconsistent with the Run-time type, it can occur-polymorphic
second, what is polymorphism
Polymorphism is "multiple forms"; in the process of running the program to decide which method to use, polymorphism is a technique that allows you to set the parent object to be equal to one or more of its child objects, and when assigned, the parent object can operate according to the attributes of its children that are currently assigned to it, which is polymorphic.
Polymorphism is the core concept in object-oriented programming field. (Object-oriented three characteristics: encapsulation, inheritance, polymorphism)
Note: Overloaded methods are determined during compilation and are static , method overloads are independent of polymorphism, and real polymorphism-related overrides (overlay override);
third, the upward transformation
A subclass is a special kind of parent class that allows a subclass object to be assigned directly to a parent class reference variable without type coercion, called an upward transition, which is done automatically by the system.
the necessary conditions for the formation of polymorphism
1, to have inheritance
2, to have rewrite (cover override)
3, the subclass object is assigned to the parent class reference variable (the parent class reference points to the subclass object)
Five, polymorphic code instances
Package com.java.test;
Class Father {public String name = ' Father '; The parent class basic method public void Fathermethod () {System.out.println () is the basic method of the parent class.
"); //Quilt class overrides method public void Overridemethod () {System.out.println ("Parent Quilt class overrides method.")
");
The public class child extends father{public String name = ' child '; Subclass Basic Method public void Childmethod () {System.out.println ("subclass basic method.")
"); The//subclass overrides the parent class method public void Overridemethod () {System.out.println () the subclass overrides the parent class method.
");
The public static void main (string[] args) {//The following compile-time type is the same as the Run-time type, and there is no polymorphic Father Father = new Father ();
System.out.println (Father.name);
Father.fathermethod ();
Father.overridemethod ();
System.out.println ();
The following compile-time type is the same as the Run-time type, and there is no polymorphic child child = new Child ();
System.out.println (Child.name);
Child.childmethod ();
Child.overridemethod ();
System.out.println (); The following compile-time types are different from run-time types, where polymorphic occurrences//parent class references point to subclass objects, polymorphic conditions are complete//father2 object compile-time type is Fatherclass, runtime type is childclass Father father2 = new Chil
D (); Note: Unlike methods, the field of an object does not have polymorphism, and the following output is the parentclass's name System.out.println (Father2.name);
Father2.fathermethod ();
Polymorphic occurrences, where the subclass rewrite method is called Father2.overridemethod (); }
}