How to do Inheritance - CSharp Custom Type

CSharp examples for Custom Type:Inheritance

Introduction

A class can inherit from another class.

Demo Code

using System;//  ww  w  .j av  a  2 s . c o m
public class Item
{
   public string Name;
}
public class Stock : Item   // inherits from Item
{
   public long SharesOwned;
}
public class Company : Item   // inherits from Item
{
   public decimal Mortgage;
}
class Test
{
   static void Main(){
      Stock msft = new Stock { Name="MSFT",
      SharesOwned=1000 };
      Console.WriteLine (msft.Name);         // MSFT
      Console.WriteLine (msft.SharesOwned);  // 1000
      Company mansion = new Company { Name="Mansion",
      Mortgage=250000 };
      Console.WriteLine (mansion.Name);      // Mansion
      Console.WriteLine (mansion.Mortgage);  // 250000
   }
}

Result


Related Tutorials