Can we have Sealed Method in abstarct class ?

 Posted by Pradsir on 2/18/2011 | Category: OOPS Interview questions | Views: 9157 | Points: 40
Answer:

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


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response

More Interview Questions by Pradsir