Are Polymorphism , Overloading and Overriding similar concepts?

70,643

Solution 1

Polymorphism can be achieved through overriding. Put in short words, polymorphism refers to the ability of an object to provide different behaviors (use different implementations) depending on its own nature. Specifically, depending on its position in the class hierarchy.

Method Overriding is when a method defined in a superclass or interface is re-defined by one of its subclasses, thus modifying/replacing the behavior the superclass provides. The decision to call an implementation or another is dynamically taken at runtime, depending on the object the operation is called from. Notice the signature of the method remains the same when overriding.

Method Overloading is unrelated to polymorphism. It refers to defining different forms of a method (usually by receiving different parameter number or types). It can be seen as static polymorphism. The decision to call an implementation or another is taken at coding time. Notice in this case the signature of the method must change.

Operator overloading is a different concept, related to polymorphism, which refers to the ability of a certain language-dependant operator to behave differently based on the type of its operands (for instance, + could mean concatenation with Strings and addition with numeric operands).

The example in Wikipedia is quite illustrative.

The following related questions might be also useful:

Solution 2

Shortly, no they are not the same.

Overloading means creating methods with same name but different parameters.

Overriding means re-defining body of a method of superclass in a subclass to change behavior of a method.

Polymorphism is a wide concept which includes overriding and overloading and much more in it's scope. Wikipedia's description of polymorphism can help you understand the polymorphism better. Especially the Subtype polymorphism (or inclusion polymorphism) section is where you should look.

Share:
70,643

Related videos on Youtube

Ammar Raja
Author by

Ammar Raja

I am Software Engineer and currently working as a .Net Developer in SherServe Technologies Faisalabad, Pakistan. Its my aim to grow myself in programming specially c# and java. Skype : ammar.asjad Cell # +92-333-9938774

Updated on July 09, 2022

Comments

  • Ammar Raja
    Ammar Raja almost 2 years

    I am very confused about the concepts of polymorphism ,overloading and overriding because it seems same to me. Please explain these concepts, and how are they different from each other

    Very confused so please guide me properly.

    Thanks

  • Daniyal Javaid
    Daniyal Javaid over 6 years
    is overloading unrelated to polymorphism ? Overloading is Compile time polymorphism . Where as over riding is runtime polymorphism
  • Xavi López
    Xavi López over 5 years
    @DaniyalJavaid a bit late to reply but I wouldn't call method overloading polymorphism of any kind when all you're doing is just invoking different methods with different signatures. All they have in common is the method name. Any method invocation could be considered compile time polymorphism under that definition.