OOPS
Mastering Inheritance in C#: The Complete Guide
Inheritance is a fundamental concept in object-oriented programming (OOP) and plays a significant role in C#. It is a mechanism that allows a new class to be based on an existing class, inheriting its properties and behaviours and also adding new ones. This article will discuss inheritance in C# and how to use it in our code.
Prerequisites
- Any basic programming language knowledge.
The article demonstrates inheritance using the C# programming language. So, to begin with, C#
Learning Objectives
- What is inheritance in C#
- What are different types of inheritance available in C#
- How to implement inheritance in C#
Getting Started
Inheritance in C#
In C#, we can create a derived class from a base class using the syntax:
class DerivedClass : BaseClass
{
// class members
}In this syntax, DerivedClass is a new class that is made, and BaseClass is the class from which it is derived. The DerivedClass class will automatically inherit all the members of the BaseClass, including fields, methods, properties, and events.
For example, let’s say we have a Person class with fields for name and age. We can create a new class Student that derives from Person and adds a new field for grade.
class Person
{
public string name;
public int age;
}
class Student : Person
{
public int grade;
}In this example, the Student class inherits the name and age fields from the Person class and adds a new grade field.
Inheritance Types
There are five types of inheritance in C#:
- Single Inheritance: A derived class inherits from a single base class.
- Multi-level Inheritance: A derived class inherits from a base class, which itself inherits from another base class.
- Hierarchical Inheritance: Multiple classes inherit from a single base class.
- Multiple Inheritance: A derived class inherits from multiple base classes.
- Hybrid Inheritance: It is a combination of any two or more types of inheritance.
Code Example: Let’s consider an example to understand inheritance in C#.
using System;
class Shape
{
public void SetWidth(int w)
{
width = w;
}
public void SetHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle : Shape
{
public int GetArea()
{
return (width * height);
}
}
// Main method
class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
rect.SetWidth(10);
rect.SetHeight(20);
// Print the area of the rectangle
Console.WriteLine("Area of Rectangle: {0}", rect.GetArea());
Console.ReadKey();
}
}In this code, we have a base class Shape, which has two methods for setting the width and height of a shape. The Rectangle class derives from Shape and adds a method GetArea() to calculate the area of a rectangle. In the Main method, we create an object of Rectangle, set its width and height, and then call the GetArea() method to calculate the area of the rectangle.
For another example, consider the following code that defines a base class Animal and a derived class Dog that inherits from Animal:
using System;
// base class
class Animal
{
public void Eat()
{
Console.WriteLine("The animal is eating.");
}
}
// derived class
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("The dog is barking.");
}
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
dog.Eat();
dog.Bark();
}
}The output of this program will be:
The animal is eating.
The dog is barking.In this example, we create an object of the Dog class and call both the Eat() method inherited from the base class Animal, and the Bark() the method defined in the Dog class. The output is the result of the WriteLine statements in each of these methods.
Conclusion
Inheritance is a powerful feature of C# that allows us to reuse code and create new classes that inherit properties and behaviours from existing classes. We can use inheritance to create a hierarchy of classes that share standard functionality and add new functionality where needed. Using inheritance, we can write more concise and maintainable code, which is essential for larger projects.
More on Inheritance
Follow me onC# Publication, LinkedIn, Instagram, Twitter, Dev.to

