Inheritance

Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows us to create new classes based on existing classes. It allows a new class, called a derived class or subclass, to inherit the characteristics and behaviors of an existing class, called a base class or superclass.

Inheritance is represented in C# using the “:” symbol followed by the name of the base class. For example:

				
					public class Animal
{
    public string Name { get; set; }
    public virtual void MakeSound()
    {
        Console.WriteLine("Generic animal sound");
    }
}
public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }
}

				
			

In this example, we have a base class called “Animal” and a derived class called “Dog”. The “Dog” class inherits from the “Animal” class using the “:” symbol. This means that the “Dog” class has access to all of the public properties and methods of the “Animal” class.

We can also override methods in the derived class, as demonstrated by the “MakeSound” method in the “Dog” class. This method overrides the base class implementation and provides a new implementation that is specific to the “Dog” class.

We can create an instance of the “Dog” class and call its methods, like this:

				
					Dog myDog = new Dog();
myDog.Name = "Fido";
myDog.MakeSound(); // Outputs "Woof!"

				
			

In this example, we create an instance of the “Dog” class and set its “Name” property to “Fido”. Then we call the “MakeSound” method on the “Dog” object, which outputs “Woof!” to the console. Because the “Dog” class inherits from the “Animal” class, it also has access to the “Name” property from the base class.

 

There are several types of inheritance in C#. Here are some examples:

  1. Single Inheritance: Single inheritance is the simplest form of inheritance, where a class inherits from only one base class. Here’s an example:
				
					public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Animal is eating");
    }
}

public class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Dog is barking");
    }
}

				
			

In this example, the “Dog” class inherits from the “Animal” class. The “Dog” class has access to the “Eat” method of the “Animal” class, and it also has its own “Bark” method.

  1. Multilevel Inheritance: Multilevel inheritance occurs when a derived class is inherited from a base class and then another class is inherited from that derived class. Here’s an example:
				
					public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Animal is eating");
    }
}
public class Mammal : Animal
{
    public void Sleep()
    {
        Console.WriteLine("Mammal is sleeping");
    }
}

public class Dog : Mammal
{
    public void Bark()
    {
        Console.WriteLine("Dog is barking");
    }
}	

				
			

In this example, the “Dog” class inherits from the “Mammal” class, which in turn inherits from the “Animal” class. The “Dog” class has access to the “Eat” method from the “Animal” class and the “Sleep” method from the “Mammal” class, in addition to its own “Bark” method.

  1. Hierarchical Inheritance: Hierarchical inheritance occurs when multiple classes inherit from the same base class. Here’s an example:
				
					public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Animal is eating");
    }
}

public class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Dog is barking");
    }
}

public class Cat : Animal
{
    public void Meow()
    {
        Console.WriteLine("Cat is meowing");
    }
}

				
			

In this example, both the “Dog” and “Cat” classes inherit from the “Animal” class. They both have access to the “Eat” method from the “Animal” class, in addition to their own unique methods.

These are just a few examples of the types of inheritance in C#. Inheritance is a powerful tool that allows us to reuse code and create complex class hierarchies.

Join To Get Our Newsletter
Spread the love