Tuesday, September 23, 2008

Override And New

Kuch to ker Zindagi ki baqa ke liye.
Zamana kuch nahi kerta kisi ke liye.

Override keyword used to override virtual method of base class.

New keyword used in case of hiding. It is used to hide base class method.


Without Override and New 

using System;
using System.Collections.Generic;
using System.Text;
namespace Demo
{
    class Base
    {
        public void Display()
        {
            Console.WriteLine("Hi : Base Class Called!");
            Console.ReadLine();
        }
    }
    class Child : Base
    {
        public void Display()
        {
            Console.WriteLine("Hi: Derived Class Called!");
            Console.ReadLine();
        }
    }
    class MainClass
    {
        public static void Main()
        {
            Base be = new Base ();
            b1.Display(); // Base
            Child c1 = new Child();
            c1.Display(); // Child
            Base b2 = new Child();
            b2.Display(); // Base
        }
    }
}

Output

Hi: Base Class Called
Hi: Derived. Class Called
Hi: Base Class Called
But will show a warning,
Use new keyword if hiding was indented.

With New Keyword

using System;
using System.Collections.Generic;
using System.Text;
namespace NewDemo
{
    class Base
    {
        public void Display()
        {
            Console.WriteLine("Hi: Base Class!");
            Console.ReadLine();
        }
    }
    class Child : Base
    {
        public new void Display()
        {
            Console.WriteLine("Hi: Derived Class!");
            Console.ReadLine();
        }
    }
    class MainClass
    {
        public static void Main()
        {
            Base b1 = new Base ();
            b1.Display (); // Base
            Child c1 = new Child();
            c1.Display(); // Child
            Base b2 = new Child();
            b2.Display(); // Base
        }
    }
}

Output

Hi: Base Class
Hi: Derived Class
Hi: Base Class

With Override Keyword

using System;
using System.Collections.Generic;
using System.Text;
namespace OverrideDemo
{
    class Base
    {
        public virtual void Display ()
        {
            Console.WriteLine("Hi: Base Class!");
            Console.ReadLine();
        }
    }
    class Child : Base
    {
        public override void Display ()
        {
            Console.WriteLine("Hi: Derived Class!");
            Console.ReadLine();
        }
    }
    class MainClass
    {
        public static void Main()
        {
            Base b1 = new Base();
            b1.Display(); // Base
            Child c1 = new Child();
            c1.Display (); // Child
            Base a2 = new Child();
            a2.Display (); // Child
        }
    }
}

Output

Hi Base Class
Hi Derived Class
Hi Derived Class

With Override and Keyword

using System;
using System.Collections.Generic;
using System.Text;
namespace OverrideNNew
{
    class Base
    {
        public virtual void Display ()
        {
            Console.WriteLine("Hi: Base Class!");
            Console.ReadLine();
        }
    }
    class Child : Base
    {
        public override void Display ()
        {
            Console.WriteLine("Hi: Derived Class!");
            Console.ReadLine();
        }
    }
    class NewClass : Child
    {
        public new void Display ()
        {
            Console.WriteLine("New method!");
            Console.ReadLine();
        }
    }
    class MainClass
    {
        public static void Main()
        {
             Base a1 = new Base();
            Console.WriteLine("Base a1 = new Base();");
            a1.Display(); // Base class

            Child b1 = new Child();
            Console.WriteLine("Child b1 = new Child();");
            b1.Display(); // Child class

           
            NewClass c1 = new NewClass();
            Console.WriteLine("NewClass c1 = new NewClass();");
            c1.Display();// New Method

            Console.WriteLine("Base a2 = new Child();");
            Base a2 = new Child();
            a2.Display(); // Child Class


            Base a3 = new NewClass();
            Console.WriteLine("Base a3 = new NewClass();");
            a3.Display(); // Child Class

            Child b3 = new NewClass();
            Console.WriteLine("Child b3 = new NewClass();");
            b3.Display(); //Child Class
        }
    }
}

Output

Hi Base Class
Hi Derived
Hi New Method
Hi Derived
Hi Derived
Hi Derived

Followers

Link