Search code examples
c#oopinheritancepolymorphism

Polymorphism fundamentals


I'm studying inheritance and polymorphism now and I've came across the concept that the compiler will evaluate (using reflection?) what type of object is stored in a base-type reference in order to decide what method to run upon calling a method with an override.

So for example:

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing shape...");
    }
}

class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing circle...");
    }
}

static void Main()
{
    Shape theShape = new Circle();
    theShape.Draw();
}

The following will be output:

Drawing circle...

It's always been my understanding that on declaring any type of object it's a way of sort of designating memory for that specific type of object. So Int32 i = 2l; would mean that I have now put memory aside as a sort of 'placeholder' for an integer. But in the code above I've put memory aside for a Shape but it can infact reference/store an object of type Circle!?


Solution

  • All class variables in C# (and in Java) are actually only references - in contrast to the so-called primitive types (e.g. int, float) and structs; the actual space for the Circle object is reserved when you write new Circle(), Shape theShape only reserves space for the reference!

    Any reference variable can hold a reference to all derived types; the actual resolution of which method to call (if it is declared virtual) happens by using virtual method tables at runtime (not via Reflection).

    To explain what polymorphism can be used for (quoting wikipedia):

    [It] allows values of different data types to be handled using a uniform interface.

    The common interface for the Shape objects, in your case would be the Draw() method. It would make perfect sense to have a list of Shapes, and calling the Draw() method on each of them to display them. Meaning, that in order to view all Shapes, your program wouldn't need to take care what kinds of Shapes are stored in this list - all the proper Draw() methods would be called automatically.

    Every class variable automatically being a reference is one of the big differences of C# (and Java) to languages like C++, where you can decide where you want your variable to live; for a Circle to be of value type (in C++), you'd write:

    Circle circle;
    

    If you instead want to point to it, you'd write

    Circle * circle = new Circle();
    

    Java and C# don't have an explicit sign making a variable a "pointer" or "reference" - simply every variable which should hold an object is a pointer/reference!

    Also note that (e.g. in C++) you can only use polymorphism if you use pointers or references; that's because value types can just be accessed as what they were declared, and not more; with references and pointers, when your actual variable is only referencing to / pointing at something, it can point to a number of things (whatever the compiler allows it to point to).