Simplest possible demonstration of inheritance. - CSharp Custom Type

CSharp examples for Custom Type:Inheritance

Description

Simplest possible demonstration of inheritance.

Demo Code

using System;//from  ww w .  j  a v  a2s.c om
public class BaseClass
{
   public int _dataMember;
   public void SomeMethod()
   {
      Console.WriteLine("SomeMethod()");
   }
}
public class SubClass : BaseClass
{
   public void SomeOtherMethod()
   {
      Console.WriteLine("SomeOtherMethod()");
   }
}
public class Program
{
   public static void Main(string[] args)
   {
      // Create a base class object.
      BaseClass bc = new BaseClass();
      bc._dataMember = 1;
      bc.SomeMethod();
      // Now create a subclass object.
      SubClass sc = new SubClass();
      sc._dataMember = 2;
      sc.SomeMethod();
      sc.SomeOtherMethod();
   }
}

Result


Related Tutorials