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
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
No comments:
Post a Comment