Udemy logo

c# inheritanceInheritance is one of the core features of an object oriented language. Inheritance fosters code reusability and organization. All the OOP languages work on the principal of mapping real world objects to software modules. A  Class typically performs this operation. However, real world objects can have several relationships between them. Inheritance is one of those relationships.

Want to learn more about OOP in C#? Take a course at Udemy.

Inheritance in the real world objects

For instance, take the example of a store that sells computer related hardware and software. Both hardware and software are products. Think of these real world objects in terms of classes. Both software and hardware products have a name, price and company which made them. These fields are common. Similarly, the class’ “Purchase” method can be common between the two classes. But, there are some properties intrinsic to software and hardware. For instance, software can have a version property; similarly it can have an Install method. Hardware can have a model property and it can have a Ship method.

The idea of inheritance is to group member variables and methods that exist in multiple classes into one parent class and then inherit that parent class in the classes which originally contained common variables and methods. All those real world objects which have ‘is a’ relationship between them can be implemented via inheritance. In the aforementioned example, both software and hardware have is a relation with a product. Software ‘is a’ product similarly hardware ‘is a’ product.

Learn more about C# Inheritance at Udemy.com

Understanding C# Inheritance

In C#, inheritance is simple to implement; the parent or the base class contains common methods and variables whereas the child or the derived classes contain unique variable and methods. A ‘:’ is used to derive a base class. In C# inheritance, a class can inherit from only one class but one class can be inherited by multiple classes. In the following example, the aforementioned inheritance relation between parent class Product and its child classes Hardware and Software, has been demonstrated.

This is how a parent class Product would look like:

Product Class

class Product
public string name;
public string type;
public int price;
public Product(string name, string type, int price)
this.name = name;
this.type = type;
this.price = price;
public void Purchase()
Console.WriteLine("You have purchased the product " + this.name);

The class has member variables name, type and price; a constructor which is used to initialize the fields and a method Purchase which simply displays the name of the product purchased by the user.

Any class which inherits the Product class will not have to write these member variables and Purchase method; rather they will be automatically available to child classes. This is how inheritance fosters reusability. The definitions of the Software and Hardware classes that inherit this parent class have been demonstrated below:

Software Class

class Software : Product
public double version;
public int size;
public Software(double version, int size, string name, string type, int price):base(name, type, price)
this.version = version;
this.size = size;
public void Install()
Console.WriteLine("Your software is being installed ...");

Hardware Class

class Hardware : Product
public double model;
public double weight;
public Hardware(double model, double weight, string name, string type, int price):base(name, type, price)
this.model = model;
this.weight = weight;
public void Ship()
Console.WriteLine("Your product will be shipped in 10 days");

You can see that both Software and Hardware class have different member variables and methods. Software class has an Install method whereas Hardware class has Ship method. The member variables of the two classes are also different. But since both of them are inheriting the Product class, they share some common methods and variables.

Another interesting thing to note here is the constructor of both Software and Hardware classes. They take 5 parameters though they initialize only two member variables. The remaining three parameters are passed to the constructor of the base class using colon operator “:” followed by “base” keyword and the parameter names. This is another effective way of calling base class constructor in C# Inheritance. In the Main method, following code can written to see how inheritance relationship actually works:

Software schild = new Software(3.2, 4000, "Game X", "Software", 100);
Hardware hchild = new Hardware(4.8, 2.5, "Laptop", "Hardware", 500);

Notice that Software class did not contain any definition for Purchase method, yet it can call it because it is inheriting Product class which contains definition for Purchase method.

Seems difficult? Learn the basics of C# at Udemy.com

Virtual Methods

In C# inheritance, Virtual methods in the parent class can be overridden by the child classes. Virtual methods are of particular advantage where derived classes want to change the definition of a method described in the parent class. If child classed do not implement their own definition of the method, then the default definition in the parent method can be used. To make a method virtual, keyword “Virtual” is used. To see how virtual methods actually affect inheritance, make following changes in the code already discussed;

Change Purchase method of the Product class as follows:

public virtual void Purchase()
Console.WriteLine("You have purchased the product " + this.name);

Notice, Purchase method has been modified with keyword virtual. Similarly, insert following method into the Software class:

public override void Purchase()
Console.WriteLine("You have purchased a software named :"+base.name);

This method will override the parent class purchase method. No changes should be made in the Hardware class. Now if Purchase method from the object of Software class is called, it will call its own overridden Purchase method, whereas if Purchase method is called from the object of Hardware class, the Purchase method of the parent class Product will be called since, Hardware class doesn’t override the virtual Purchase method of the Product class.

In C#, inheritance is a vast domain; it includes abstract classes, abstract methods, interfaces, casting, sealing and many more advanced concepts.

To study C# Inheritance concepts in detail, take this course.

Page Last Updated: April 2014

Top courses in C# (programming language)

Ultimate C# Masterclass for 2024
Krystyna Ślusarczyk
4.7 (2,929)
Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (41,817)
.NET / C# Interview Questions with Answers.
Shivprasad Koirala
4.7 (1,676)
C#/.NET - 50 Essential Interview Questions (Mid Level)
Krystyna Ślusarczyk
4.9 (269)
Highest Rated
Complete C# Masterclass
Denis Panjuta, Tutorials.eu by Denis Panjuta
4.5 (29,652)
Design Patterns in C# and .NET
Dmitri Nesteruk
4.5 (12,002)
C# 10 | Ultimate Guide - Beginner to Advanced | Master class
Web University by Harsha Vardhan
4.6 (3,679)
The Unity C# Survival Guide
Jonathan Weinberger, Unity Technologies, GameDevHQ Team
4.8 (1,796)

More C# (programming language) Courses

C# (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo