Friday, November 30, 2018

Method Hiding and Method Overriding


//Shadowing replaces the complete element of the parent.In simple word it can change a
//variable element of parent to a method or function

//Shadowing  is useful when get a requirement where the vocabulary will be kept
//same but the data type needs to be change.

//Overriding changes implementation while Shadowing replaces element with a

//complete new element only the interface vocabulary is same


//Method Hiding
namespace MethodHiding
{
    class A
    {
        public void PrintName()
        {
            Console.WriteLine("Print A");
        }
    }
    class B : A
    {
        public new void PrintName()
        {
            Console.WriteLine("Print B");
        }
    }
    class C : B
    {
        public new void PrintName()
        {
            Console.WriteLine("Print C");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            objB.PrintName();//B

            C objC = new C();
            objC.PrintName();//C

            A objAB = new B();
            objAB.PrintName();//A

            B objCB = new C();
            objCB.PrintName();//B
        }
    }
}

//Method Overriding
namespace MethodOverriding
{
    class A
    {
        public virtual void PrintName()
        {
            Console.WriteLine("Print A");
        }
    }
    class B : A
    {
        public override void PrintName()
        {
            Console.WriteLine("Print B");
        }
    }
    class C : B
    {
        public override void PrintName()
        {
            Console.WriteLine("Print C");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            objB.PrintName();//B

            C objC = new C();
            objC.PrintName();//C

            A objAB = new B();
            objAB.PrintName();//B


            B objCB = new C();
            objCB.PrintName();//C
        }
    }
}

No comments:

Post a Comment

Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...