Q)What is output of C# code?
sealed class X
{
public void f1()
{
Console.WriteLine("Base f1()");
}
}
class Y : X
{
public void f2()
{
Console.WriteLine("Derived f2()");
}
}
class Program
{
static void Main(string[] args)
{
Y obj = new Y();
obj.f2();
}
}
Answer:
Output: Compiler error
Explanation: Class Y is derived from a sealed class. In C# object oriented programming a sealed class cannot be inherited. Recommended to read more on Sealed class and sealed method in C# and real time use of sealed class in C# programming.
Q)What is issue with below c# multilevel inheritance implementation?
class L1
{
public virtual void f1()
{
Console.WriteLine("L1:f1()");
}
}
class L2 : L1
{
public sealed override void f1()
{
Console.WriteLine("L2:f1()");
}
}
class L3 : L2
{
public override void f1()
{
Console.WriteLine("L3:f1()");
}
}
Answer:
Since, in L2 class f1() method is sealed it cannot be overridden in further derived class from L2. in L3 class f1() should not be overridden. Sealed method in C# programming is the feature we use to stop further overriding of a method of a class.