Polymorphic understanding - this piece is enough

A method name with different parameters is called method overloading. (Overload)

Parent instance = new Child();
instance.foo(); //==> Child foo()

void foo(String str);
void foo(int number);

The parent class and the child class have the same method name and parameters, which is called method overriding. (Override)

class Parent {
    void foo() {
        System.out.println("Parent foo()");
    }
}
class Child extends Parent {
    void foo() {
        System.out.println("Child foo()");
    }

The reference of the parent class points to the object of the child class. When calling a method, the implementation of the child class is called instead of the implementation of the parent class. This is called polymorphism.

Parent instance = new Child();
instance.foo(); //==> Child foo()
Polymorphism as I understand it has to do with inheritance, it has to do with method overriding and has nothing to do with method overloading.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324476065&siteId=291194637