Analysis of Java polymorphism

Java polymorphism

Today, let’s talk about polymorphism in Java. As a major object-oriented feature, its importance needless to say. Compared with the other two features (inheritance and encapsulation), it is a bit difficult to read literally, and multiple attitudes are still How abnormal?

Official solution

Official explanation:

Polymorphism is the ability to have multiple different manifestations or forms of the same behavior.

Polymorphism is the same interface, using different instances to perform different operations.

The simple understanding is that the same method has different implementations in different classes (inheritance relationship), or the reference of the parent class points to the subclass object;

Here I attach the official diagram:

Insert picture description here

As shown in the figure: a printer has the function of printing , but different printers can print different effects (such as color, black) in different scenarios or different needs.

Furthermore: For example, if you have a bank card and need to withdraw money at the counter, you can submit the bank card to your friends or relatives to help you with it, and you can still withdraw the money, because during the process of withdrawing money, The person in charge of the counter or the ATM machine does not know that you are the one holding the card; this process is a process of polymorphism.

Advantages of polymorphism

Now that you understand what polymorphism is, then you must understand what polymorphism is used for, in other words, what are the advantages of polymorphism:

  • Reusability:

    When we write programs, it is not necessary to create the same method or function for each subclass, we only need to deal with the abstract parent class.

  • Improve scalability and maintainability:

    The function of the subclass can be called by the method or reference variable of the parent class.

Necessary conditions for existence

  • Inheritance or realization

  • Rewrite

  • The parent class reference points to the child class object: such as: Parent p = new Child();

    The above three necessary conditions must be met at the same time. When using a polymorphic method to call a method, you must first check whether the method is in the parent class. If not, a compilation error will occur;

    If so, call the method of the same name in the subclass.

Format: parent class variable name = new subclass

​ Variable name. Method name ()

public class Dad {
    
    
    void method() {
    
    
        System.out.println("我是父亲");
    }
}

class Son extends Dad {
    
    
    @Override
        //实现重写父类的method()方法
    void method() {
    
    
        System.out.println("我是儿子");
    }
    void play(){
    
    
        System.out.println("打篮球");
    }
}

class Dau extends Dad {
    
    
    @Override
        //实现重写父类的method()方法
    void method() {
    
    
        System.out.println("我是女儿");
    }
}

class test {
    
    
    //实现多态,父类的引用指向子类对象
    public static void main(String[] args) {
    
    
        Dad dad = new Son();
        //父类       子类
        dad.method();

        dad = new Dau();
        dad.method();
    }
}

Output result:

I am a son
i am a daughter

Conversion of reference data types

The transformation of reference data types is divided into two types: upward transformation and downward transformation;

  • Upcasting refers to the automatic conversion of subclass reference types to parent class reference types to achieve polymorphism.
  • Downcasting refers to forcing the reference type of the parent class to the reference type of the subclass, which is to call the unique method of the subclass.

The above code is an upward transformation and achieves polymorphism;

class test {
    
    
    //向下转型,调用Son的方法
    public static void main(String[] args) {
    
    
        Dad dad = new Son();
        Son s = (Son) dad;
        //子类              父类
        s.play();
    }
}

Output result:

play basketball

The above code is a downward transformation, calling the unique play method of the subclass .


Guess you like

Origin blog.csdn.net/ITMuscle/article/details/111054766