Home  >  Article  >  Java  >  How to understand polymorphism in java

How to understand polymorphism in java

(*-*)浩
(*-*)浩Original
2019-05-21 19:53:4255693browse

How to understand polymorphism in java

Polymorphism: Polymorphism refers to allowing objects of different subtypes to respond differently to the same message. Simply put, the same method is called with the same object reference but different things are done. Polymorphism is divided into compile-time polymorphism and run-time polymorphism.

If the methods of an object are regarded as the services provided by the object to the outside world, then runtime polymorphism can be explained as: when system A accesses the services provided by system B, system B has multiple methods of providing services. method, but everything is transparent to system A (just like an electric shaver is system A, and its power supply system is system B. System B can be powered by batteries or AC, or even solar energy. A The system will only call the power supply method through Class B objects, but it does not know what the underlying implementation of the power supply system is and how it obtains power).

Method overloading (overload) implements compile-time polymorphism (also known as front binding), while method overriding (override) implements runtime polymorphism (also known as post binding).

Runtime polymorphism is the essence of object-oriented. To achieve polymorphism, two things need to be done:

  • Method rewriting (The subclass inherits the parent class and overrides existing or abstract methods in the parent class);

  • Object modeling (use the parent type reference to refer to the subtype object, so that the same reference call The same method will show different behaviors depending on the subclass object).

java中的引用变量有两个类型:一个编译时类型,一个是运行时类型。编译时类型由声明该变量使用的类型决定,运行时类型由实际赋给
该变量的对象决定。如果编译时类型与运行时类型不一致,就可能出现所谓的多态。——疯狂java讲义

Examples of use of runtime polymorphism are as follows:

public class PolyTest5
{
    public void run(Car car)//这样利用公共的父类就屏蔽了子类之间的差异性,可以应对所有的车.
    {
        car.run();//只要子类继承了car都可以作为参数传入方法中.如果没有多态,就要有对应每个类的方法.非常的麻烦.
    }

    public static void main(String[] args)
    {

        PolyTest5 test = new PolyTest5();

        Car car = new BMW();

        test.run(car);

        QQ qq = new QQ();

        test.run(qq);//向上类型转换.


    }
}

class Car
{
    public void run()
    {
        System.out.println("car is running");
    }
}

class BMW extends Car
{
    public void run()
    {
        System.out.println("BMW is running");
    }
}

class QQ extends Car
{
    public void run()
    {
        System.out.println("QQ is running");
    }
}

The three elements of polymorphism: 1. Inheritance 2. Rewrite 3. The parent class reference points to the child class object.

Related learning recommendations: java basic tutorial

The above is the detailed content of How to understand polymorphism in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn