Thursday 9 April 2015

OOPS : Can we have Sealed Method in abstarct class ?

Looking at first site the The Keyword Sealed & Abstract are contradictory to each other..In simple terms we can Say Answer is NO...

Look the code below

using System;

abstract class A
{
public abstract void Hello();
public sealed void Hi();
}

when we will complie the code.. we will get the Compile time Error as below

'A.Hi()' cannot be sealed because it is not an override..

But the Crux is We can have Sealed methods in abstract class when the abstract class is Dervided class .. for Eg.

using System;

class A
{
public virtual void Hello()
{
Console.WriteLine(" Say Hello");
}

}

abstract class B : A
{
public sealed override void Hello()
{
Console.WriteLine(" Say Hi");
}

}

class C : B
{

}


class Demo
{
public static void Main()
{
C c1 = new C();
c1.Hello();// Output is Say Hi
}
}


// Thanks

OOPS : New vs Override keyword

using System;

using System.Data;

using System.Text;

using System.Windows.Forms;


namespace BaseDerive

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {


            BaseClass b = new BaseClass();

            b.func1();


            DeriveClass d = new DeriveClass();

            d.func1();


            //Calls Base class function 1 as new keyword is used.

            BaseClass bd = new DeriveClass();

            bd.func1();


            //Calls Derived class function 2 as override keyword is used.

            BaseClass bd2 = new DeriveClass();

            bd2.func2();


        }

    }


    public class BaseClass

    {

        public virtual void func1()

        {

            MessageBox.Show("Base Class function 1.");

        }


        public virtual void func2()

        {

            MessageBox.Show("Base Class function 2.");

        }


  public void func3()

        {

            MessageBox.Show("Base Class function 3.");

        }

    }


    public class DeriveClass : BaseClass

    {

        public new void func1()

        {

            MessageBox.Show("Derieve Class fuction 1 used new keyword");

        }


        public override void func2()

        {

            MessageBox.Show("Derieve Class fuction 2 used override keyword");

        }


public void func3()

        {

            MessageBox.Show("Derieve Class fuction 3 used override keyword");

        }


    }

}

This is a window application so all the code for calling the function through objects is written in Form_Load event.
As seen in above code, I have declared 2 classes. One works as a Base class and second is a derieve class derived from base class.

Now the difference is

new: hides the base class function.
Override: overrides the base class function.


BaseClass objB = new DeriveClass();


If we create object like above notation and make a call to any function which exists in base class and derive class both, then it will always make a call to function of base class. If we have overidden the method in derive class then it wlll call the derive class function.

For example…

objB.func1(); //Calls the base class function. (In case of new keyword)

objB.func2(); //Calls the derive class function. (Override)

objB.func3(); //Calls the base class function.(Same prototype in both the class.)

Note:
// This will throw a compile time error. (Casting is required.)
DeriveClass objB = new BaseClass(); 


//This will throw run time error. (Unable to cast)
DeriveClass objB = (DeriveClass) new BaseClass();