Sunday 4 November 2012

Program to show the use of abstract keyword in Inheritance


using System;
abstract class Base
{
 public Base()
 {
  Console.WriteLine("Welcome in Base Class");
 }
 public void Display()
 {
  Console.WriteLine("It's a normal method in abstract class");
 }
 public abstract void Sum(int x,int y);
}
class Derived : Base
{
 public Derived()
 {
  Console.WriteLine("Welcome in Derived Class");
 }
 public override void Sum(int x,int y)
 {
  Console.WriteLine("Sum of {0} and {1} is {2}.",x,y,x+y);
 }
}
class AbstractTest
{
 static void Main()
 {
  /*Base b=new Base();
  b.Display();*/        //Error; cannot create an instance of abstract class or interface 'Base'
  Derived d=new Derived();
  d.Display();
  d.Sum(12,30);
  Console.ReadKey();
 }
}

Output:-

No comments:

Post a Comment