Is polymorphism another term for overloading?

2019-01-21 18:02发布

问题:

Is polymorphism another term for overloading?

回答1:

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");
     }
}


回答2:

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.



回答3:

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.



回答4:

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");