What is polymorphism, and overloading method overrides

What is polymorphic

Concept of polymorphism it is relatively simple, the same operation is applied to different objects can have different interpretations, resulting in different execution results.

If you follow this concept to define, then it should be a multi-state operation of the state. In order to achieve runtime polymorphism, or that is dynamic binding, need to meet three conditions.

Class that is inherited or interface, the parent class subclasses to override, the reference to the parent class pointing object subclass.

public class Parent{
    
    public void call(){
        sout("im Parent");
    }
}

public class Son extends Parent{// 1.有类继承或者接口实现
    public void call(){// 2.子类要重写父类的方法
        sout("im Son");
    }
}

public class Daughter extends Parent{// 1.有类继承或者接口实现
    public void call(){// 2.子类要重写父类的方法
        sout("im Daughter");
    }
}

public class Test{
    
    public static void main(String[] args){
        Parent p = new Son(); //3.父类的引用指向子类的对象
        Parent p1 = new Daughter(); //3.父类的引用指向子类的对象
    }
}

重载(静态多态)
就是函数或者方法有同样的名称,但是参数列表不相同的情形,这样的同名不同参数的函数或者方法之间,相互称之为重载函数或者方法。

重写(动态多态)
就是Java的子类与父类中有两个名称,参数列表都相同的方法的情况。由于他们具有相同的方法签名,所以子类中的新方法将覆盖父类中原有的方法。
 

Guess you like

Origin www.cnblogs.com/lujiahua/p/11407637.html