OOPS
Mastering Inheritance in C#: Single Inheritance
In C#, inheritance is a powerful feature that allows developers to create new classes based on existing classes. With inheritance, a new class, called a derived class or subclass, can inherit the properties and behaviours of an existing class, called a base class or superclass. Single inheritance is a type in which a derived class inherits from a single base class. In this article, we will explore the concept of single inheritance in C# in more depth, including how to use it with examples.
Prerequisites
- Any basic programming language knowledge.
The article demonstrates inheritance using the C# programming language. So, to begin with, C#
Learning Objectives
- How to implement single inheritance in C#
Getting Started
Single Inheritance in C#
Single inheritance is a simple way to create a new class based on an existing class. In single inheritance, a derived class inherits all of the members of a single base class, including its methods, properties, and fields. The derived class can also define new members, such as additional methods or properties, that are not present in the base class. This allows developers to create new classes similar to existing classes but with other functionality or modifications.
In C#, single inheritance is implemented using the colon (:) symbol to indicate that a class is derived from another class. For example, consider the following code that defines a base class Animal and a derived class Dog:
class Animal
{
public void Eat()
{
Console.WriteLine("The animal is eating.");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("The dog is barking.");
}
}In this example, we define a base class Animal with a single method Eat(). We then define a derived class Dog that inherits from Animal. The Dog class also defines a new method Bark(), which is not present in the Animal class.
Using Single Inheritance in C#
To use single inheritance in C#, we create a new instance of the derived class and call its methods or properties. The derived class will automatically inherit all of the members of its base class, including any methods or properties defined in the base class. For example, consider the following code that creates an instance of the Dog class and calls its Eat() and Bark() methods:
Dog myDog = new Dog();
myDog.Eat(); // output: "The animal is eating."
myDog.Bark(); // output: "The dog is barking."In this example, we create a new instance of the Dog class and call its Eat() and Bark() methods. The output of these methods is determined by the WriteLine statements in each method. Because Dog inherits from Animal, it also inherits the Eat() method defined in the Animal class.
Overriding Base Class Members
In addition to inheriting members from the base class, a derived class can also override or modify the behaviour of a base class member. To do this, the derived class must define a method with the same name and signature as the base class member. This is known as method overriding.
For example, consider the following modified code that adds a virtual method MakeSound() to the Animal class, which is then overridden in the Dog class:
class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound.");
}
public void Eat()
{
Console.WriteLine("The animal is eating.");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("The dog barks.");
}
public void Bark()
{
Console.WriteLine("The dog is barking.");
}
}
}In this example, we add a virtual method MakeSound()to the Animal class that prints a generic message indicating that the animal makes a sound. We then override this method in the Dog class to print a message showing that the dog barks instead. Note that we use the override keyword to show that the MakeSound() method in Dog is overriding the virtual MakeSound() method in Animal.
Now, when we call the MakeSound() method on an instance of the Dog class, we get the following output:
Dog myDog = new Dog();
myDog.MakeSound(); // output: "The dog barks."In this case, the MakeSound() method defined in the Dog class overrides the virtual MakeSound() method defined in the Animal class. When we call MakeSound() on an instance of the Dog class, we get the message "The dog barks." instead of "The animal makes a sound.".
Final Thoughts
Single inheritance is a powerful and widely used feature in object-oriented programming that allows developers to create new classes based on existing classes. By using single inheritance in C#, developers can easily extend the functionality of existing classes and create new classes tailored to their specific needs.
In this article, we have explored the concept of single inheritance in C#, including how to use it with examples. We have also seen how to override base class members in a derived class. With a solid understanding of single inheritance, you can begin to create more complex and powerful object-oriented programs in C#.
More on Inheritance
Follow me onC# Publication, LinkedIn, Instagram, Twitter, Dev.to

