tags:

views:

540

answers:

5

Is polymorphism another term for overloading?

+19  A: 

No; overloading is creating a method with the same name with a different amount of parameters, or with parameters which are of another type.

Polymorphism is about changing the implementation / functionality of a specific method across various types (which all have the same 'base-type').

Overloading:

public class TestClass
{
    public void DoSomething( int a, int b ) {}

    public void DoSomething( int a, int b, string x ) {}
}

Polymorphism:

public abstract class Base
{
    public abstract DoSomething();
}

public class A : Base
{
    public override DoSomething()
    {
         Console.WriteLine("I am A");
    }
}

public class B : Base
{
     public override DoSomething()
     {
         Console.WriteLine("I am B");
     }
}
Frederik Gheysels
This has to be done via an interface correct?
Not necessarily an interface. A common base class would be enough.
sharptooth
It can be done with an interface as well as a base or abstract class. The point is that it doesn't matter what the specific type of the object is. It just needs to be indistinguishable from the base for the purposes of using the base; whatever the base may be.
Brendan Enrick
It can be an interface, it can be an abstract class, it can be a regular base class.Note though, that the method has to be 'virtual' in order to be able to override it.
Frederik Gheysels
Based on your polymorphism example, @Frederik, you should define the class `base` as `abstract`.
dboarman
+5  A: 

The difference between polymorphism and method overloading is in the time when the actual method to execute is determined. The reason for this is that when a method is overloaded, such as in:

account = new BankAccount();
account = new BankAccount(1000);

The compiler can tell which constructor to use by the method signature, including the number and types of parameters provided. This selection of a method to use at compile time, before the program ever runs is called early binding. On the other hand, when we use a polymorphic method call such as x.getMeasure() the actual getMeasure() method called depends on what type of object x refers to. Because objects are not constructed until the program runs, the method called is determined at run-time. Therefore, the virtual machine, not the compiler selects the appropriate method. This method selection is called late-binding.

Ahmy
A: 

No, it is not.

Overloading refers to creating a method or an operator with the same name, but different parameters and - depending on the language - different return types.

Overriding refers to reimplementing a method with the same signature in a derived class and enables polymorphism - the decision what implementation of an overwritten method to call is made at runtime depending on the actual type of the object.

class BaseClass
{
     public void DoStuff(Int32 value) { } // Overloading

     public void DoStuff(String value) { } // Overloading

     public virtual void DoOtherStuff(String value) { }
}

class DerivedClass : BaseClass
{
    public override void DoOtherStuff(String value) { } // Overriding
}

Usage example

BaseClass instance = null;

if (condition)
{
    instance = new BaseClass();
}
else
{
    instance = new DerivedClass();
}

// Using overloads
instance.DoStuff(4);
instance.DoStuff("four");

// Polymorphism - it depends on the actual type of the object
// referenced by the variable 'instance' if BaseClass.DoOtherStuff()
// or DerivedClass.DoOtherStuff() will be called at runtime.
instance.DoOtherStuff("other stuff");
Daniel Brückner
+1  A: 

No.

Polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. The concept of parametric polymorphism applies to both data types and functions. A function that can evaluate to or be applied to values of different types is known as a polymorphic function. A data type that can appear to be of a generalized type (e.g., a list with elements of arbitrary type) is designated polymorphic data type like the generalized type from which such specializations are made.

Method overloading is a feature found in various programming languages such as Ada, C#, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function.

Method overloading should not be confused with type polymorphism or virtual functions. In those, the correct method is chosen at runtime.

Source: Wikipedia.

Daniel Daranas