Search This Blog

Monday 18 January 2016

Method overriding Vs Method Hiding in c sharp

Method overriding – Using override keyword in child class, Re-implementing the logic of base class method and hiding the base method.

Method Hiding – Using new keyword in child class hiding the own method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodOverridingVsHiding
{
    class BaseClass
    {
        public BaseClass()
        {
            Console.WriteLine("Base Class Constructor.");
        }

        public void Print()
        {
            Console.WriteLine("Base Class Print Method.");
        }

        public void BaseDisplay()
        {
            Console.WriteLine("Base Class Display Method.");
        }
    }

    class ChildClass : BaseClass
    {
        public ChildClass()
        {
            Console.WriteLine("Child Class Constructor.");
        }

        public void Print()
        {
            Console.WriteLine("Child Class Print Method.");
        }

        public void ChildDisplay()
        {
            Console.WriteLine("Child Class Display Method.");
        }

        static void Main(string[] args)
        {
            // create object here

            Console.ReadLine();
        }
    }
}

Step 1:- Create object of Base Class

BaseClass objBaseClass = new BaseClass();
objBaseClass.Print();
objBaseClass.BaseDisplay();

Output

Base Class Constructor.
Base Class Print Method.
Base Class Display Method.

Note:-

1.  If you create the object of the base class, it will access all method in the base class.
2.  If the class using constructor, it will call first and then the method calling sequence.

Step 2:- Create object of Child Class

ChildClass objChildClass = new ChildClass();
objChildClass.Print();
objChildClass.BaseDisplay();
objChildClass.ChildDisplay();

Output

Base Class Constructor.
Child Class Constructor.
Child Class Print Method.
Base Class Display Method.
Child Class Display Method.

Note:-

1.  If a child class inherits the base class and you are creating the object of the child class, its access all method of the base class as well as its own method.
2.  If base class any method with same name exists in the child class, it’s called the own method.
3.  If you want to still call the base method use the new keyword, new keyword tells the compiler to hide the child method and call the base method.
4.  If the classes using constructor, it will call first base class constructor and then the child constructor.
             
Note (For Point 3):

1.  If you are using the new keyword in child class for hiding method intentional, check base class method marked virtual or not.
2.  If base class method not marked virtual, still it's called child class method.

Do following Changes:

In Base class mark Print method as virtual
        public virtual void Print()
        {
            Console.WriteLine("Base Class Print Method.");
        }
In Child class mark Print method as new
        public new void Print()
        {
            Console.WriteLine("Base Class Print Method.");
        }


Step 3:- Create object of Base Class that reference to object of Child class

BaseClass objBaseChild = new ChildClass();
objBaseChild.Print();
objBaseChild.BaseDisplay();

Output

Base Class Constructor.
Child Class Constructor.
Base Class Print Method.
Base Class Display Method.

Note:-

1.  If you create the object of the base class that reference to object of a child class, its access the all method of the base class as well as overriding methods of the child.
2.  If base class any method with same name exists in the child class, it’s called the base class method.
3.  If you want to still call the child method use the override keyword, override keyword tells the compiler to hide the base method and call the child method.
4.  If the classes using constructor, it will call first base class constructor and then the child constructor.

Note (For Point 3):

1.  If you are using the override keyword in child class, check base class method marked virtual or not.
2.       If base class method not marked virtual, you will get the runtime error.

Do following Changes:

In Base class mark Print method as virtual
        public virtual void Print()
        {
            Console.WriteLine("Base Class Print Method.");
        }
In Child class mark Print method as new
        public override void Print()
        {
            Console.WriteLine("Base Class Print Method.");

        }

No comments:

Post a Comment