Web Design that taps into the haromny and vision of your dreams.

Class Inheritance in C#

How class inheritance works

Written By on in C#

484 words, estimated reading time 3 minutes.

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.

C# Programming Series
  1. Introduction to Programming
  2. What is C#?
  3. Your First Console Application in C#
  4. Introducing Methods and the Main() Function in C#
  5. Introducing C# Classes and Structs
  6. C# Data Types, Variables and Casting
  7. C# Program Flow Control and Entry Points
  8. Passing Parameters to Methods and Return Values in C#
  9. C# Access Modifiers and Scope
  10. C# Interfaces and Classes
  11. Using Namespaces in C#
  12. C# Conditional Statements
  13. Looping and Iteration in C#
  14. Using Arrays and Lists in C#
  15. C# Constants and Read-Only Variables
  16. Error and Exception Handling in C#
  17. Using Recursion in C#
  18. C# Operator List
  19. Class Inheritance in C#
  20. C# Class and Method Attributes Explained
  21. C# Class Constructors and Destructors
  22. C# Generics Variables
  23. XML Serialization and Deserialization
  24. C# String Formatting Examples

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.

class baseCarClass
  public string Colour;
  public int numberOfSeats;
  public int direction;
  public string modelName;
  public baseEngineClass  engine;
class baseEngineClass
  public string name;
  public int numberOfCylinders;
  public int MPG;
  public int acceleration;

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.

public class SportsCar : baseCarClass

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.

public class SportsCar : baseCarClass
  public bool isRoofUp;
  public void RaiseRoof()
    isRoofUp = true;
  public void DropRoof()
    isRoofUp = false;

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

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!

public class PoliceCar : baseCarClass
  public void Siren()
    // Make some noise!
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.

Last updated on: Saturday 24th June 2017




Where does the "Color" data type get defined?

Reply to M&M


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me