views:

245

answers:

7

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.

+1  A: 

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());
    }
}
Abel
I don't think polymorphisme = overloading + overriding, overloading is really non-object-oriented related. You can have overloading using C.
Clement Herreman
@Clement: I think his final sentence may have just been poorly worded (I think "this" was intended to apply to overriding, not overloading).
Adam Robinson
@Clement / @Adam: you are both right: overloading is a core concept of OO (but also of non-OO langauges, check OOSC page 95), but not of polymorphism per se. However, language implementers must deal with overloading resolutions when resolving polymorphism issues. I removed the ambiguity.
Abel
+1  A: 

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)
thebackhand
+3  A: 

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.

dark_charlie
Thanks a lot! I have read that article again and everything is quite clear now.
Hai Minh Nguyen
+1  A: 

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.

Clement Herreman
Just as a point of fact, overloading does not have to take place in the same class (child classes are free to define additional overloads).
Adam Robinson
@Adam Robinson: yup you're right, I update that to make it clear.
Clement Herreman
+2  A: 

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.

mikera
A: 

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.

Alex Humphrey
A: 

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.

thalm