Polymorphism, Method Hiding and Overriding in C#

Overview

One of the fundamental concepts of object oriented software development is polymorphism. The term polymorphism (from the Greek meaning “having multiple forms”) in OO is the characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow a variable to refer to more than one type of object.

Inherited Methods

A method Sadi() which is declared in the base class A and not redeclared in classes B or C is inherited in the two subclasses

    using System;
    namespace Polymorphism
    {
        class A
        {
            public void Sadi() { Console.WriteLine("A::Sadi()"); }
        }

        class B : A {}

        class Test
        {
            static void Main(string[] args)
            {
                A a = new A();
                a.Sadi();  // output --> "A::Sadi()"

                B b = new B();
                b.Sadi();  // output --> "A::Sadi()"
            }
        }
    }
      

The method Sadi() can be overridden in classes B and C:

    using System;
    namespace Polymorphism
    {
        class A
        {
              public void Sadi() { Console.WriteLine("A::Sadi()"); }
        }

        class B : A
        {
              public void Sadi() { Console.WriteLine("B::Sadi()"); }
        }

        class Test
        {
            static void Main(string[] args)
            {
                A a;
                B b;

                a = new A();
                b = new B();
                a.Sadi();  // output --> "A::Sadi()"
                b.Sadi();  // output --> "B::Sadi()"

                a = new B();
                a.Sadi();  // output --> "A::Sadi()"
            }
        }
    } 

This issue will be discussed in section Hiding and Overriding Methods.

Methods (Use of override keyword)Virtual and Overridden

Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.

    using System;
    namespace Polymorphism
    {
        class A
        {
            public virtual void Sadi() { Console.WriteLine("A::Sadi()"); }
        }

        class B : A
        {
            public override void Sadi() { Console.WriteLine("B::Sadi()"); }
        }

        class Test
        {
            static void Main(string[] args)
            {
                A a;
                B b;

                a = new A();
                b = new B();
                a.Sadi();  // output --> "A::Sadi()"
                b.Sadi();  // output --> "B::Sadi()"

                a = new B();
                a.Sadi();  // output --> "B::Sadi()"
            }
        }
     } 

Method Hiding (Use of new keyword)

Why did the compiler in the second listing generate a warning? Because C# not only supports method overriding, but also method hiding. Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method has to be declared using the new keyword. The correct class definition in the second listing is thus:

    using System;
    namespace Polymorphism
    {
        class A
        {
            public void Sadi() { Console.WriteLine("A::Sadi()"); }
        }

        class B : A
        {
            public new void Sadi() { Console.WriteLine("B::Sadi()"); }
        }

        class Test
        {
            static void Main(string[] args)
            {
                A a;
                B b;

                a = new A();
                b = new B();
                a.Sadi();  // output --> "A::Sadi()"
                b.Sadi();  // output --> "B::Sadi()"

                a = new B();
                a.Sadi();  // output --> "A::Sadi()"
            }
        }
    } 
Advertisements

4 thoughts on “Polymorphism, Method Hiding and Overriding in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s