Website development and design blog, tutorials and inspiration

Class Inheritance in C#

How class inheritance works

By , 7th November 2007 in C#

Class inheritance is one of the key principles in object oriented programming. It allows one class to pass on properties and methods to child classes so that they can reuse, extend, and modify existing functionality.
 

In simplistic terms, class inheritance allows one class to inherit methods, properties and fields from a parent class and to also build upon its functionality. Classes inherit from another class using a colon followed by the class name to inherit from on the class declaration line.

In this tutorial we will be using the car class code examples from the object orientation tutorial and extending the base car class to create some new types of cars which are more specialised.

Here is a reminder of the baseCarClass from that tutorial.

  1. class baseCarClass
  2. {
  3. public string Colour;
  4. public int numberOfSeats;
  5. public int direction;
  6. public string modelName;
  7. public baseEngineClass engine;
  8. }
  9.  
  10. class baseEngineClass
  11. {
  12. public string name;
  13. public int numberOfCylinders;
  14. public int MPG;
  15. public int acceleration;
  16. }

In this baseCarClass we can define all the properties and methods that are common to ALL car classes. We can then create a derived class which will have more specialised code. From the baseCarClass we are going to create a more specialised class, which is more tailored to our needs. In this example, let's create a sports car class which has a folding roof.

  1. public class SportsCar : baseCarClass
  2. {
  3. }

This basic class definition should be familiar, with the additional inheritance " : baseCarClass" bit added on. Basically this is saying that we want a new class called SportsCar which contains everything that baseCarClass contains.

Next we can add the folding roof as a boolean property, with two methods, one to raise the roof and the other to lower it.

  1. public class SportsCar : baseCarClass
  2. {
  3. public bool isRoofUp;
  4.  
  5. public void RaiseRoof()
  6. {
  7. isRoofUp = true;
  8. }
  9.  
  10. public void DropRoof()
  11. {
  12. isRoofUp = false;
  13. }
  14. }

We can now create an instance of the sports car class and use it with the code below:

  1. SportsCar myCar = new SportsCar();

If you look at the myCar object with IntelliSense, you will notice that not only does it have methods for Raise and Lower roof, but also those from the base class.

We can also create a different type of car in the same manner, all of which share the same properties of the base class, and more. Let's create a Police Car!

  1. public class PoliceCar : baseCarClass
  2. {
  3. public void Siren()
  4. {
  5. // Make some noise!
  6. }
  7. }
  1. PoliceCar copper = new PoliceCar();

If you look at copper with IntelliSense, you will notice that it has a method called Siren, but not Raise and Lower roof.

Access Modifiers

A derived class has access to the public, protected, internal, and protected internal members of a base class. Even though a derived class inherits the private members of a base class, it cannot access those members. However, all those private members are still present in the derived class and can do the same work they would do in the base class itself. For example, suppose that a protected base class method accesses a private field. That field has to be present in the derived class for the inherited base class method to work correctly.

Comments
  1. M&M
    M&M

    Where does the "Color" data type get defined?

Leave a Reply

Your email address will not be published.