Sunday, 4 November 2012

Program to show the upcasting of an object of Derived class to an object of its base class


using System;
class Maruti
{
 public virtual void Display()
 {
  Console.WriteLine("Maruti Car.");
 }
}
class Esteem : Maruti
{
 public override void Display()
 {
  Console.WriteLine("Maruti Esteem.");
 }
}
class Zen : Maruti
{
 public override void Display()
 {
  Console.WriteLine("Maruti Zen.");
 }
}
class Inclusion
{
 static void Main()
 {
  Maruti m=new Maruti();
  m.Display();
  m=new Esteem();    //Upcasting
  m.Display();
  m=new Zen();       //Upcasting
  m.Display();
  Console.ReadKey();
 }
}

Output:-

No comments:

Post a Comment