CSharp/C# Tutorial - C# Inheritance

A class can inherit from another class to extend or customize the original class.

Inheriting from a class reuses the functionality in that class.

A class can inherit from only a single class.


In this example, we start by defining a class called Item:

public class Item {
   public string Name; 

Next, we define classes called Product and DiscountProduct, which will inherit from Item.

Product and DiscountProduct get everything an Item has, plus any additional members that they define:

public class Product : Item{ // inherits from Item 

    public long InStoreCount; 
public class DiscountProduct : Item{  // inherits from Item 

    public decimal Price; 

Here's how we can use these classes:

Product myProduct = new Product {Name="CSS",InStoreCount=1000}; 

Console.WriteLine (myProduct.Name); // CSS 
Console.WriteLine (myProduct.InStoreCount); // 1000 

The following code uses the DiscountProduct class.

DiscountProduct mansion = new DiscountProduct { Name="SQL", Price=250000 }; 
Console.WriteLine (mansion.Name);     // SQL 
Console.WriteLine (mansion.Price); // 250000 


The derived classes, Product and DiscountProduct, inherit the Name property from the base class, Item.

A derived class is also called a subclass. A base class is also called a superclass.


References are polymorphic.

This means a variable of type x can refer to an object that subclasses x.

For instance, consider the following method:

public static void Display (Item i) { 
    System.Console.WriteLine (i.Name); 

This method can display both a Product and a DiscountProduct, since they are both Items:

Product myProduct = new Product ... ; 
DiscountProduct mansion = new DiscountProduct ... ; 
Display (myProduct); 
Display (mansion); 

Polymorphism works since subclasses have all the features of their base class.

The is operator

The <code>is</code> operator tests whether an 
object derives from a specified class or implements an interface. 

It is often used to test before downcasting. 

if (a is Product) {
   Console.WriteLine (((Product)a).InStoreCount); 

Virtual Function Members

A function marked as virtual can be overridden by subclasses wanting to provide a specialized implementation.

Methods, properties, indexers, and events can all be declared virtual:

public class Item {
   public string Name;
   public virtual decimal FinalPrice { get { return 0; } } 

A subclass overrides a virtual method by applying the override modifier:

public class Product : Item {
    public long InStoreCount; 
} //  w ww .  j a  v a2  s . com
public class DiscountProduct : Item {
    public decimal Price;
    public override decimal FinalPrice { 
       get { 
          return Price; 

By default, the FinalPrice of an Item is 0.

A Product does not need to specialize this behavior.

However, the DiscountProduct specializes the FinalPrice property to return the value of the Price:

DiscountProduct mansion = new DiscountProduct { Name="SQL", Price=250000 }; 
Item a = mansion; 
Console.WriteLine (mansion.FinalPrice); // 250000 
Console.WriteLine (a.FinalPrice); // 250000 

The signatures, return types, and accessibility of the virtual and overridden methods must be identical.

An overridden method can call its base class implementation via the base keyword.