Polymorphism: Method hiding and overriding

Creating a method in derived class with same signature as a method in base class is called as method overriding.
Same signature means methods must have same name, same number of arguments and same type of arguments.
Method overriding is possible only in derived classes, but not within the same class.
When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.
To allow the derived class to override a method of the base class, C# provides two options,virtual methods and abstract methods.
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract.

Method Hiding

If a method is not overriding the derived method, it is hiding it in the Derived class.  A hiding method has to be declared using the new keyword.

class A
        {
            public void Foo() { Console.WriteLine(“A::Foo()“); }
        }

        class B : A {}

        class Test
        {
            static void Main(string[] args)
            {
                A a = new A();
                a.Foo();  // output –> “A::Foo()”

                B b = new B();
                b.Foo();  // output –> “A::Foo()”
            }
        }

————————————————————————————-

class A
        {
              public void Foo() { Console.WriteLine(“A::Foo()“); }
        }

        class B : A
        {
              public void Foo() { Console.WriteLine(“B::Foo()“); }
        }

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

                a = new A();
                b = new B();
                a.Foo();  // output –> “A::Foo()”
                b.Foo();  // output –> “B::Foo()”

                a = new B();
                a.Foo();  // output –> “A::Foo()”
            }

  • Although the code compiles and runs, the compiler produces a warning:

…\polymorphism.cs: warning : The keyword new is required on ‘Polymorphism.B.Foo()’ because it hides inherited member ‘Polymorphism.A.Foo()’

—————————————————————-

Virtual and Overridden Methods

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.

class A
        {
            public virtual void Foo() { Console.WriteLine(“A::Foo()“); }
        }

        class B : A
        {
            public override void Foo() { Console.WriteLine(“B::Foo()“); }
        }

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

                a = new A();
                b = new B();
                a.Foo();  // output –> “A::Foo()”
                b.Foo();  // output –> “B::Foo()”

                a = new B();
                a.Foo();  // output –> “B::Foo()”
            }
        }

——————————————————————————————–
Method Hiding

If a method is not overriding the derived method, it is hiding it in the Derived class.  A hiding method has to be declared using the new keyword.

class A
        {
            public void Foo() { Console.WriteLine(“A::Foo()“); }
        }

        class B : A
        {
            public new void Foo() { Console.WriteLine(“B::Foo()“); }
        }

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

                a = new A();
                b = new B();
                a.Foo();  // output –> “A::Foo()”
                b.Foo();  // output –> “B::Foo()”

                a = new B();
                a.Foo();  // output –> “A::Foo()”
            }

Combining Method Overriding and Hiding

Methods of a derived class can both be virtual and at the same time hide the derived method. In order to declare such a method, both keywords virtual and new have to be used in the method declaration:

            class A
            {
                public void Foo() {}
            }

            class B : A
            {
                public virtual new void Foo() {}
            }     

A class C can now declare a method Foo() that either overrides or hides Foo() from class B:

            class C : B
            {
                public override void Foo() {}
                // or
                public new void Foo() {}
            }

Advertisements

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