Tutorial :Why does Wikipedia say “Polymorphism is not the same as method overloading or method overriding.”



Question:

I have looked around and could not find any similar question.

Here is the paragraph I got from Wikipedia:

Polymorphism is not the same as method overloading or method overriding. Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class. Method overloading refers to methods that have the same name but different signatures inside the same class. Method overriding is where a subclass replaces the implementation of one or more of its parent's methods. Neither method overloading nor method overriding are by themselves implementations of polymorphism.

Could anyone here explain it more clearly, especially the part "Polymorphism is not the same as method overriding"? I am confused now. Thanks in advance.


Solution:1

Polymorphism (very simply said) is a possibility to use a derived class where a base class is expected:

class Base {    }    class Derived extends Base  {    }    Base v = new Derived(); // OK  

Method overriding, on the other hand, is as Wiki says a way to change the method behavior in a derived class:

class Shape  {    void draw() { /* Nothing here, could be abstract*/ }  }    class Square extends Shape  {    @Override    void draw() { /* Draw the square here */ }  }  

Overloading is unrelated to inheritance, it allows defining more functions with the same name that differ only in the arguments they take.


Solution:2

You can have polymorphism in a language that does not allow method overriding (or even inheritance). e.g. by having several different objects implement the same interface. Polymorphism just means that you can have different concrete implementations of the same abstract interface. Some languages discourage or disallow inheritance but allow this kind of polymorphism in the spirit of programming with abstractions.

You could also theoretically have method overriding without polymorphism in a language that doesn't allow virtual method dispatch. The effect would be that you could create a new class with overridden methods, but you wouldn't be able to use it in place of the parent class. I'm not aware of any mainstream language that does this.


Solution:3

Polymorphism is not about methods being overridden; it is about the objects determining the implementation of a particular process. An easy example - but by no means the only example - is with inheritance:

A Novel is a type of Book. It has most of the same methods, and everything you can do to a Book can also be done to a Novel. Therefore, any method that accepts a Book as an argument can also deal with a Novel as an argument. (Example would include .read(), .write(), .burn()). This is, per se, not referring to the fact that a Novel can overwrite a Book method. Instead, it is referring to a feature of abstraction. If a professor assigns a Book to be read, he/she doesn't care how you read it - just that you do. Similarly, a calling program doesn't care how an object of type Book is read, just that it is. If the object is a Novel, it will be read as a Novel. If it is not a novel but is still a book, it will be read as a Book.

Book:

private void read(){    #Read the book.  }  

Novel:

private void read(){    #Read a book, and complain about how long it is, because it's a novel!    }  

Overloading methods is just referring to having two methods with the same name but a different number of arguments. Example:

writeNovel(int numPages, String name)    writeNovel(String name)  


Solution:4

Overloading is having, in the same class, many methods with the same name, but differents parameters.

Overriding is having, in an inherited class, the same method+parameters of a base class. Thus, depending on the class of the object, either the base method, or the inherited method will be called.

Polymorphism is the fact that, an instance of an inherited class can replace an instance of a base class, when given as a parameters.

E.g. :

class Shape {    public void draw() {      //code here    }    public void draw(int size) {      //this is overloading     }  }    class Square inherits Shape {    public void draw() {      //some other code : this is overriding    }      public void draw(color c) {      //this is overloading too    }  }    class Work {    public myMethod(Shape s) {      //using polymophism, you can give to this method      //a Shape, but also a Square, because Square inherits Shape.    }  }  

See it ? Polymorphing is the fact that, the same object, can be used as an instance of its own class, its base class, or even as an interface.


Solution:5

Polymorphism refers to the fact that an instance of a type can be treated just like any instance of any of its supertypes. Polymorphism means 'many forms'.

Say you had a type named Dog. You then have a type named Spaniel which inherits from Dog. An instance of Spaniel can be used wherever a Dog is used - it can be treated just like any other Dog instance. This is polymorphism.

Method overriding is what a subclass may do to methods in a base class. Dog may contain a Bark method. Spaniel can override that method to provide a more specific implementation. Overriding methods does not affect polymorphism - the fact that you've overriden a Dog method in Spaniel does not enable you to or prevent you from treating a Spaniel like a dog.

Method overloading is simply the act of giving different methods which take different parameters the same name.

I hope that helps.


Solution:6

Frankly:

Polymorphism is using many types which have specific things in common in one implementation which only needs the common things, where as method overloading is using one implementation for each type.


Solution:7

When you override a method, you change its implementation. Polymorphism will use your implementation, or a base implementation, depending on your language (does it support virtual methods?) and depending on the class instance you've created.

Overloading a method is something else, it means using the same method with a different amount of parameters.

The combination of this (overriding), plus the possibility to use base classes or interfaces and still call an overriden method somewhere up the chain, is called polymorphism.

Example:

interface IVehicle  {      void Drive();  }    class Car : IVehicle  {      public Drive() { /* drive a car */ }  }    class MotorBike : IVehicle  {      public Drive() { /* drive a motorbike */ }  }    class Program  {      public int Main()      {          var myCar = new Car();          var myMotorBike = new MotorBike();          this.DriveAVehicle(myCar);        // drive myCar          this.DriveAVehicle(myMotorBike);  // drive a motobike          this.DriveAVhehicle();            // drive a default car      }        // drive any vehicle that implements IVehicle      // this is polymorphism in action      public DriveAVehicle(IVehicle vehicle)      {         vehicle.Drive();      }        // overload, creates a default car and drives it      // another part of OO, not directly related to polymorphism      public DriveAVehicle()      {          // typically, overloads just perform shortcuts to the method          // with the real implemenation, making it easier for users of the class          this.DriveAVehicle(new Car());      }  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »