IMPORTANT PLEASE READ!
Website development and design blog, tutorials and inspiration

Class Inheritance in C#

How class inheritance works

By , Written on in C#

Class Inheritance 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.

  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.

Last updated on: Saturday 24th June 2017

Did you Like this Post? Why not Like us on Facebook?

 

Comments
  1. M&M
    M&M

    Where does the "Color" data type get defined?

Leave a Reply

Your email address will not be published.