The way to Java -- Day12 (polymorphism)

Posted by RDKL PerFecT on Sat, 26 Oct 2019 20:20:33 +0200

2019-10-26-22:40:09

Catalog:

1. The concept of polymorphism

2. Classification of polymorphism

3. Three necessary conditions for polymorphism

4. Polymorphic format

5. Characteristics of polymorphic member variables

6. Characteristics of polymorphic member method

7. Benefits of polymorphism

8. Upward and downward transformation of polymorphism

1. The concept of polymorphism:

Polymorphism is another important feature of object-oriented programming. It means that after the properties and methods defined in the parent class are inherited by the subclass, they can have different data types or behave differently, which makes the same property or method have different meanings in the parent class and its subclasses.

2. Classification of polymorphism:

Compile time polymorphism (static): mainly refers to the overloading of methods. It distinguishes different methods according to the different parameter lists.

Runtime polymorphism (dynamic): it is implemented by dynamic binding, which is commonly known as polymorphism.

3. Three necessary conditions to realize polymorphism:

1. Inheritance: there must be subclass and parent class with inheritance relationship in polymorphism.

2. Rewrite: the subclass redefines some methods in the parent class. When these methods are called, the subclass's methods will be called.

3. Upward Transformation: in polymorphism, it is necessary to assign the reference of the subclass to the parent object. Only in this way can the reference call both the methods of the parent and the methods of the subclass.

 1 package demosummary.polymorphic;
 2 
 3 public class Fu {
 4     //Define two variables
 5     double d1;
 6     double d2;
 7 
 8     //Non parametric structure
 9     public Fu() {
10     }
11 
12     //All parameter structure
13     public Fu(double d1, double d2) {
14         this.d1 = d1;
15         this.d2 = d2;
16     }
17 
18     //Set a method
19     public void area(double d1,double d2){
20     }
21 }
 1 package demosummary.polymorphic;
 2 //Create a subclass that inherits the parent
 3 public class Zi extends Fu{
 4     //Override method in parent class
 5     @Override
 6     public void area(double d1, double d2) {
 7         double result = d1 * d2;
 8         System.out.println("Calculation results:"+result);
 9     }
10 
11     //test method
12     public static void main(String[] args) {
13         //Upward transformation(Polymorphic writing)
14         Fu obj = new Zi();
15         obj.area(3.5,3.5);
16     }
17 }

4. Polymorphic format:

Parent name object name = new child name ();

Interface name object name = new implementation class name ();

5. Characteristics of polymorphic member variables:

1. Access member variables directly through the object name: see who is on the left side of the equal sign and who is preferred. If not, look up.
2. Access the member variables indirectly through the member method: see who the method belongs to and who has priority. If not, look up.

 1 package demosummary.polymorphic;
 2 //Define a parent class
 3 public class Fu01 {
 4     //Define a member variable
 5     int num = 10;
 6     //Define a member method
 7     public void showNum(){
 8         //Define a local variable
 9 //        int num01 = 100;
10         System.out.println(num);
11 //        System.out.println(num01);
12     }
13 }
 1 package demosummary.polymorphic;
 2 
 3 public class Zi01 extends Fu01{
 4     //Define a member variable
 5     int num = 20;
 6     //Override override parent method
 7     @Override
 8     public void showNum() {
 9         //Define a local variable
10 //        int num01 = 200;
11         System.out.println(num);
12 //        System.out.println(num01);
13     }
14     //Define a test class
15     public static void main(String[] args) {
16         Fu01 obj = new Zi01();
17         System.out.println(obj.num);
18         obj.showNum();
19     }
20 }

6. Characteristics of polymorphic member method:

In polymorphic code, the access rules of member methods are:
To see who the new is, it's better to use it first. If not, it's better to look up.
Pithy formula: compile to the left, run to the right.
Compare:
Member variables: compile to the left, run to the left.
Member method: compile to the left, run to the right.

7. Benefits of polymorphism:

Do not use polymorphic writing:

    Teacher one = new Teacher();

    one.work();

    Assistant two = new Assistant();

    two.work();

Use polymorphic writing:

    Employee one = new Teacher();

    one.work();

    Employee two = new Assistant();

    two.work();

Comparison conclusion: no matter which subclass object is changed when new is on the right, the call method on the left side of the equal sign will not change

8. Polymorphic upward and downward Transformation:

Upward Transformation:

Parent class name object name = new child class name();

Meaning: create a subclass object on the right and use it as a parent class

Note: upward transformation must be safe, from small scope to large scope

Transition down:

Subclass name object name = (subclass name) parent object;

Meaning: restore the parent object to the original child object

Precautions

1. When the object is created, it must be guaranteed. It's a cat. To be a cat.
2. If the object was not originally a cat when it was created, and now it has to be transformed down to a cat, an error will be reported.

Topics: Java Programming