Monday, 5 November 2012

Program to show the inheritance using abstract class


using System;
class Program
{
 static void Main()
 {
  ABS[] aArray = new ABS[3];
  aArray[0] = new Class1(1,2,"This is the first value in array");
  aArray[1] = new Class1(3,4,"This is the next value in array");
  aArray[2] = new Class2(5,6);
  for(int i=0;i<3;aArray[i].Show(),i++);
  Console.ReadKey();
 }
}
public abstract class ABS
{
 public ABS(int top,int left)
 {
  this.top = top;
  this.left = left;
 }
 public abstract void Show();
 protected int top,left;
}
public class Class1 : ABS
{
 public Class1(int top,int left,string contents) : base(top,left)
 {
  class1values = contents;
 }
 public override void Show()
 {
  Console.WriteLine("This is overriding the method Show declared in\n abstract class ABS : "+class1values+"\n");
 }
 private string class1values;
}
public class Class2 : ABS
{
 public Class2(int top,int left) : base(top,left){}
 public override void Show()
 {
  Console.WriteLine("This is again overriding the method Show declared in\n abstract class ABS in Class2.\n");
 }
}

Output:-

No comments:

Post a Comment