Overloading is compile-time polymorphism. Really?

后端 未结 5 1519
面向向阳花
面向向阳花 2020-12-02 17:11

I do know the syntactical difference between overriding and overloading. And I also know that overriding is run-time polymorphism and overloading is compile-time polymorphis

5条回答
  •  暖寄归人
    2020-12-02 18:08

    What is polymorphism?

    Acc. to me: if an entity can be represented in more than one forms, that entity is said to exhibit polymorphism.

    Now, lets apply this definition to Java constructs:

    1) Operator overloading is compile time polymorphism.

    For example, + operator can be used to add two numbers OR to concatenate two strings. it's an example of polymorphism strictly saying compile-time polymorphism.

    2) Method overloading is compile time polymorphism.

    For example, a method with same name can have more than one implemntations. it's also a compile-time polymorphism.

    It's compile-time because before execution of program compiler decides the flow of program i.e which form will be used during run-time.

    3) Method overriding is run-time polymorphism.

    For example, a method with same signature can have more than one implemenations. it's a run time polymorphism.

    4) Base class use in place of derived class is run time polymorphism.

    For example, an interface reference can point to any of it's implementor.

    It's run-time because the flow of program can't be known before execution i.e. only during run-time it can be decided that which form will be used.

    I hope it clears a bit.

提交回复
热议问题