Inheritance is one of the most fundamental concepts of Object-Oriented-Programming (OOP). Inheritance fosters program modularization, enhances code integrity and, increases code reusability. The concept of inheritance in programming is borrowed from real-world inheritance. A child inherits certain characteristics from its parents and also has some of its unique characteristics that are not borrowed from parents. In the same way, in object-oriented programming, inheritance allows a class to inherit some properties and methods from other classes.
A class that inherits characteristics e.g. methods and attributes from another class is called a derived, or a child class. On the other hand, the class which is inherited by other classes is called a base or parent class. Inheritance implements an Is-A relation between a child and parent class. For example, if you have classes Shape and Circle, the relationship will be “A Circle is-a Shape” where the Circle will be a child class while the Shape will be a parent class. In this article, you will study inheritance in C# with examples. So, let’s being without ado.
Table of Contents
- A Simple Example of Inheritance
- Adding Constructors to Child Classes
- Initializing Parent Class Constructor via Child Classes
A Simple Example of Inheritance
While some programming languages such as Python allow multiple inheritances, in C# a child class can only inherit from one parent class. On the contrary, one parent class can be inherited by multiple child classes. Let’s see a simple example of inheritance in Python.
In the following script, you create three classes X, Y, and Z. The X class contains two variables and one method. Both the Y and Z classes contain one variable and one method each. The Y and Z classes inherit the X class which makes X a parent class and Y and Z, its child classes.
You can see that to inherit from a class, you have to append a colon and the parent class name with the child class name while defining the child class. For instance, to make the class Y inherit from X, you can use the notation “Y : X“.
class X { public int a = 10; public int b = 5; public void ParentFunction() { Console.WriteLine("This is a function in the parent class"); } } class Y : X { public int c = 100; public void ChildYFunction() { Console.WriteLine("This is a function in the Child Y class"); } } class Z : X { public int c = 200; public void ChildZFunction() { Console.WriteLine("This is a function in the Child Z class"); } }
A child class can access the public and protected attributes and methods of a parent class. In the above script, since classes Y and Z inherit the X class, the Y and Z classes can directly call the parent X class method ParentFunction(). Similarly, the Y and Z classes can also access and modify the attributes a and b.
In the following script, you create objects of the X, Y, and Z classes. Using the X class object you call the ParentFunction() and also print the variables a and b. Using the Y class object, you first call the X class function ParentFunction() and then call the ChildYFunction() which is a function inside the child class Y. The parent class variables a and b are also accessed using the Y class object. Similarly, using the Z class object, you first call the X class function ParentFunction() and then call the ChildZFunction() which is a function inside the child class Z.
class Program { static void Main(string[] args) { // Accessing parent class functions from the parent class X x = new X(); x.ParentFunction(); Console.WriteLine(x.a); Console.WriteLine(x.b); // Accessing both parent and child class functions from Child class Y Y y = new Y(); y.ParentFunction(); y.ChildYFunction(); Console.WriteLine(y.c); // Accessing both parent and child class functions from Child class Z Z z = new Z(); z.ParentFunction(); z.ChildZFunction(); Console.WriteLine(z.c); Console.ReadKey(); } }
Output
Adding Constructors to Child Classes
A class can have a constructor function as well. A constructor function is a function that has the same name as the class in which it is declared. A constructor function has no return type. In this section, you will create constructor functions inside child classes Y and Z. Later you will see how you can create a constructor function inside a parent class and then initialize the parent class constructor using a child class constructor.
The following script creates constructor functions for Y and Z classes. The constructor functions initialize the variable c in both the Y and Z classes.
class Y : X { public int c; public Y(int var1) { c = var1; } public void ChildYFunction() { Console.WriteLine("This is a function in the Child Y class"); } } class Z : X { public int c; public Z(int var1) { c = var1; } public void ChildZFunction() { Console.WriteLine("This is a function in the Child Z class"); } }
Now when you create objects of the child classes Y and Z, you need to pass values for constructors that will initialize variables c in both the classes. Notice that you do not need to change anything for the parent class X.
class Program { static void Main(string[] args) { // Accessing parent class functions from the parent class X x = new X(); x.ParentFunction(); Console.WriteLine(x.a); Console.WriteLine(x.b); // Accessing both parent and child class functions from Child class Y Y y = new Y(500); y.ParentFunction(); y.ChildYFunction(); Console.WriteLine(y.c); // Accessing both parent and child class functions from Child class Y Z z = new Z(100); z.ParentFunction(); z.ChildZFunction(); Console.WriteLine(z.c); Console.ReadKey(); } }
In addition to other values, the output now contains the values for variables c (500 and 100), initialized using constructors of classes Y and Z.
Output:
Initializing Parent Class Constructor via Child Classes
Creating a constructor for a class is straight forward. For example, remove the classes Y and Z from the previous script and modify the class X as follows.
class X { public int a; public int b; public X (int var1, int var2) { a = var1; b = var2; } public void ParentFunction() { Console.WriteLine("This is a function in the parent class"); } }
The class X in the above script contains one constructor which initializes variables a and b.
Inside the Main method, you can create an object of the X class and pass values for the a and b variables via the X class constructor as shown in the following script:
class Program { static void Main(string[] args) { // Accessing parent class functions from the parent class X x = new X(99, 101); x.ParentFunction(); Console.WriteLine(x.a); Console.WriteLine(x.b); Console.ReadKey(); } }
Output:
Now consider a scenario where a parent class has a constructor. How to pass values to parent class constructors via child classes? The situation becomes trickier if the child classes have their own constructors too.
To tackle this situation, you need to define a child class constructor. The parameters for both child and parent class constructors are passed to the child constructor. Next, you need to append a colon (:) followed by the keyword base after the child class constructor name. The base keyword is followed by opening and closing round brackets and the values for the parent class constructor are passed via the base function. Look at the following script:
class X { public int a; public int b; public X (int var1, int var2) { a = var1; b = var2; } public void ParentFunction() { Console.WriteLine("This is a function in the parent class"); } } class Y : X { public int c; public Y(int var1, int var2, int var3) : base(var2, var3) { c = var1; } public void ChildYFunction() { Console.WriteLine("This is a function in the Child Y class"); } } class Z : X { public int c; public Z(int var1, int var2, int var3) : base(var2, var3) { c = var1; } public void ChildZFunction() { Console.WriteLine("This is a function in the Child Z class"); } }
In the script above, the parent class X constructor accepts two parameter values. Next, inside the constructors of classes Y and Z, three-parameter values are passed: var1, var2, var3. Out of these values, the var1 value initializes the variables c of Y and Z classes. The parameters values for var2 and var3 are passed to the base() method which calls the parent class X constructor, which in turn initializes its variables a and b using the constructor parameters var2 and var3.
Inside the Main method, when you create an object of a child’s Y or Z, you need to pass three values for class constructors. The first value will initialize child class variable c, while the remaining two values will initialize the X class variables a and b.
Look at the following script for reference.
class Program { static void Main(string[] args) { // Accessing parent class functions from the parent class X x = new X(99, 101); x.ParentFunction(); Console.WriteLine(x.a); Console.WriteLine(x.b); // Accessing both parent and child class functions from Child class Y Y y = new Y(500, 1000,2000); y.ParentFunction(); y.ChildYFunction(); Console.WriteLine(y.c); Console.WriteLine(y.a); Console.WriteLine(y.b); // Accessing both parent and child class functions from Child class Y Z z = new Z(100, 2000, 4000); z.ParentFunction(); z.ChildZFunction(); Console.WriteLine(z.c); Console.WriteLine(z.a); Console.WriteLine(z.b); Console.ReadKey(); } }
Output: