author : Martin Raphael.K
// email : mrkraj@officetiger.com
// Date : November 02, 2001 
// This is the sample code for inheritence
// new is the keyword which is used to override the base(Parent) class
//members base is the keyword whichs is equal to super in java
using System;
public class Base
{
 public string str;
 public Base()
 {
  Console.WriteLine("Base class Constructor");
 }
 ~Base()
 {
  Console.WriteLine("Base class Destructor");
 }
 public void getData()
 {
  Console.WriteLine("Base class GetData");
 }
}
public class Derived:Base
{
 public new string str;
 public Derived()
 {
  Console.WriteLine("Derived class Constructor");
 }
 ~Derived()
 {
  Console.WriteLine("Derived class Destructor");
 }
 public new void getData()
 {
  base.getData();
  Console.WriteLine("Derived class GetData");
 }
 public void getValues()
 {
  base.str=Console.ReadLine();
  this.str=Console.ReadLine();
 }
 public void printValues()
 {
  Console.WriteLine("Base class Str "+base.str);
  Console.WriteLine("Derived class Str "+this.str);
 }
}
class InheritenceDemo
{
 public static void Main(string[] a)
 {
  Derived d=new Derived();
  d.getData();
  d.getValues();
  d.printValues();
  Console.WriteLine("———–");
  // this is used to get the Base class getData Method
  ((Base)d).getData();
 }
}
// send a feedback to mrkraj@officetiger.com
 
No comments:
Post a Comment