C# Tutorial - Inheritance

posted Jun 28, 2011, 7:25 PM by magic stone

Inheritance represents the relationship between two objects A and B, such that B 'is a kind of' A. In UML, we show this by using the triangle symbol. We can use the objects that we have already been looking at to expand on this concept. We have a 'person' object and a 'dog' object, so how are they related? A 'dog' is an animal, more specifically a 'dog' is a mammal. A 'person' is also a mammal, so we have found a common parent for the two objects. As we have seen in previous pages, the 'person' and 'dog' objects share an amount of common information and subsequently common behaviour. It is this common data that they both get from the parent object, so we could create an object that has the common data within it.


In the previous diagram, I have tried to show one possible hierarchical structure that will allow the 'person' object and the 'dog' object to share common data logically. However, the diagram shows a flaw in our thinking, 'dogs' can be domestic or wild. A 'wild' dog will not have a name or address, so we need to clarify each type of 'dog' object with a unique identity. So we may have our 'dog' object representing domestic dogs, while we change the object name to 'WildDog' for the other object of type 'dog'.

Finally I have shown that we can inherit from our dog objects into individual breeds of dog if we wished to.

In general when talking about inheritance, we talk about a type of object. An object inherits from a parent, known as its super-type. A child is inherited from an object and is known as a sub-type.

Coding Inheritance

You create a derived class by adding a colon after the name of the derived class, followed by the name of the base class:

public class Terrier : Dog

This code declares a new class, Terrier derived from Dog. You can read the colon as "derives from." The derived class inherits all the members of the base class, both member variables and methods. The derived class is free to implement its own version of a base class method or variable. It does so by marking it with the keyword new. This indicates that the derived class has intentionally hidden and replaced the base class member.

class Dog
{
  private string species = "Dog";
  public virtual string getSpecies() { return species; }
}

class Terrier : Dog
{
  new private string species = "Terrier";
  public override string getSpecies() { return species; }
}

The species variable above overrides the base class variable of the same name.

Dog myDog = new Dog();
Terrier myTerrier = new Terrier();

Console.WriteLine("The species of myDog is {0}", myDog.getSpecies());
Console.WriteLine("The species of myTerrier is {0}", myTerrier.getSpecies());

// Output
The species of myDog is Dog
The species of myTerrier is Terrier

Calling Base Class Constructors

The Terrier constructor would invoke the constructor of its parent by placing a colon (:) after the parameter list and then invoking the base class with the keyword base. If the base class has an accessible default constructor, the derived constructor is not required to invoke the base constructor, as the default constructor is called implicitly. If the base class does not have a default constructor, every derived constructor must explicitly invoke one of the base class constructors using the base keyword.

public Terrier() : base()

Calling Base Class Methods

If a class needs to call a method in the base class then this is achieved using the base keyword followed by the method required in the usual manner using dot notation.

base.getSpecies();
Comments