Sunday, 4 November 2012

Program to show the use of sealed keyword for class in inheritance and overriding


using System;
sealed class GrandParent
{
 public void Show()
 {
  Console.WriteLine("GrandParent Class Method");
 }
}
class Parent // :GrandParent        //Error; 'Parent' cannot derive from sealed type class 'GrandParent'
{
 public virtual void Display()
 {
  Console.WriteLine("Parent Class Method");
 }
}
sealed class Child : Parent
{
  public override void Display()
  {
   Console.WriteLine("Child Class Method");
   base.Display();
  }
}
class HideTest
{
 static void Main()
 {
  GrandParent g=new GrandParent();
  g.Show();
  Parent p=new Parent();
  p.Display();
  Child d=new Child();
  d.Display();
  Console.ReadKey();
 }
}

Output;-

No comments:

Post a Comment