Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism/Overriding

Could someone please explain what the difference is between these two examples?

Class A

protected virtual string GetData()

Class B

private override string GetData()

And the following:

Class A

protected string GetData()

Class B

private string GetData()

Assuming that 'Class B' inherits from 'Class A'.

I always assumed that you need to use virtual in the superclass and override in the subclass if you want overriding of a method, however I tried removing the keywords and the program compiled fine. What exactly is the difference, if any?

like image 572
Dot NET Avatar asked Jan 11 '12 23:01

Dot NET


People also ask

Is polymorphism overriding or overloading?

Method overloading is a compile-time polymorphism. Method overriding is a run-time polymorphism. It helps to increase the readability of the program. It is used to grant the specific implementation of the method which is already provided by its parent class or superclass.

Is polymorphism a function overriding?

Function overriding is a part of runtime polymorphism. In function overriding, more than one method has the same name with different types of the parameter list. It is achieved by using virtual functions and pointers. It provides slow execution as it is known at the run time.


2 Answers

The second example that you showed hides the GetData of the parent, it doesn't override it.

Example:

private class Base
{
    public virtual void Test()
    {
        Console.WriteLine("Base");
    }

    public void Test2()
    {
        Console.WriteLine("Base");
    }
}

private class Derived : Base
{
    public override void Test()
    {
        Console.WriteLine("Derived");
    }

    public void Test2()
    {
        Console.WriteLine("Derived");
    }
}

static void Main()
{
    Base b = new Base();
    Derived d = new Derived();
    Base dInB = new Derived();

    b.Test();
    d.Test();
    dInB.Test();

    b.Test2();
    d.Test2();
    dInB.Test2();

    Console.ReadKey(true);
}

It outputs:

Base    // Base.Test()
Derived // Derived.Test()
Derived // Derived.Test()
Base    // Base.Test2()
Derived // Derived.Test2()
Base    // You think you're calling Derived.Test2(), but you actually call Base.Test2()

Actually this sample is invalid, because it should use the new keyword in public new void Test2() in the Derived class.

It works just like operator overloading. It doesn't actually override anything. When you have the exact type Derived it calls the new method.

You have to be really careful with hiding members, it is nothing like overriding (classes) or implementing (interfaces) at all. Only when you have the exact type it'll call a new method, otherwise it'll still call the base type's method!

like image 145
Aidiakapi Avatar answered Oct 12 '22 14:10

Aidiakapi


The difference is that in the first case you are overriding and in the second case you are hiding which is completely different.

In the first case:

class B: A
{
    void Foo()
    {
        B b = new B();
        A a = b;

        a.GetData() //B's GetData() will be called
        b.GetData() //B's GetData() will be called
    }
}

On the other hand in the second case:

class B: A
{
    void Foo()
    {
        B b = new B();
        A a = b;

        a.GetData() //A's GetData() will be called
        b.GetData() //B's GetData() will be called
    }
}

In the second case you are simply hiding A's implementation of GetData() but you will always be able to call A's implementation through a variable typed A even if the variable refers to a instance of type B. Note that this is completely different from how overriding behaves.

like image 40
InBetween Avatar answered Oct 12 '22 13:10

InBetween