C# Inheritance

Inheritance in C#

What is Inheritance?

Inheritance is a key idea in object-oriented programming (OOP). It lets one class use the features and behaviours of another class. This creates a parent-child relationship between classes.

How Does It Work?

In C#, you can create a base class (or parent class) with common features. Other classes, called derived classes (or child classes), can use these features.

Why Use Inheritance?

  1. Reusability: Use the same code in different places.
  2. Organization: Group related features together.
  3. Specialization: Create specialized versions of a class.
Commonly use Inheritance
  1. Single Inheritance: A class inherits from one base class.
Single inheritance :

public class Animal
{
    public void Eat()
    {
        Console.WriteLine(“Animal is eating.”);
    }
}
public class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine(“Dog is barking.”);
    }
}

Explanation:

public class Animal
{

This line defines a new class called ‘Animal’.
A class is like a blueprint for creating objects with specific features and behaviors.

public void Eat()

This line defines a method called ‘Eat’ inside the ‘Animal’ class.
A method is a block of code that performs a specific task.

Console.WriteLine(“Animal is eating.”);

This line prints the text “Animal is eating.” to the console.

public class Dog : Animal

This line defines a new class called ‘Dog’ that inherits from the ‘Animal’ class by using colon sign”:”.
Inheriting means ‘Dog’ will have all the features of ‘Animal’ plus its own features.

public void Bark()

This line defines a method called ‘Bark’ inside the ‘Dog’ class.
The ‘Bark’ method is specific to the ‘Dog’ class.

Console.WriteLine(“Dog is barking.”);

This line prints the text “Dog is barking.” to the console.

2. Hierarchical Inheritance: Multiple classes inherit from one base class
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.”);
    }
}

Explanation:

public class Animal
{

This line defines a new class called ‘Animal’.
A class is like a blueprint for creating objects with specific features and behaviors.

public void Eat()

This line defines a method called ‘Eat’ inside the ‘Animal’ class.
A method is a block of code that performs a specific task.

Console.WriteLine(“Animal is eating.”);

This line prints the text “Animal is eating.” to the console.

public class Dog : Animal

This line defines a new class called ‘Dog’ that inherits from the ‘Animal’ class by using colon – “:”.
Inheriting means ‘Dog’ will have all the features of ‘Animal’ plus its own features.

public void Bark()

This line defines a method called ‘Bark’ inside the ‘Dog’ class.
The ‘Bark’ method is specific to the ‘Dog’ class.

Console.WriteLine(“Dog is barking.”);

This line prints the text “Dog is barking.” to the console.

public class Cat : Animal

This line defines a new class called ‘Cat’ that also inherits from the ‘Animal’ class by using colon – “:”.
This means ‘Cat’ will have all the features of ‘Animal’ plus its own features.

public void Meow()

This line defines a method called ‘Meow’ inside the ‘Cat’ class.
The ‘Meow’ method is specific to the ‘Cat’ class.

Console.WriteLine(“Cat is meowing.”);

This line prints the text “Cat is meowing.” to the console.

Multilevel Inheritance: A class inherits from a derived class.
Example

public class Animal
{
    public void Eat()
    {
        Console.WriteLine(“Animal is eating.”);
    }
}
public class Mammal : Animal
{
    public void Breathe()
    {
       Console.WriteLine(“Mammal is breathing.”);
    }
}
public class Dog : Mammal
{
    public void Bark()
    {
        Console.WriteLine(“Dog is barking.”);
    }
}

Explanation:

public class Animal
{

This line defines a new class called ‘Animal’.
A class is like a blueprint for creating objects with specific features and behaviors.

public void Eat()
    {

This line defines a method called ‘Eat’ inside the ‘Animal’ class.
A method is a block of code that performs a specific task.

Console.WriteLine(“Animal is eating.”);

This line prints the text “Animal is eating.” to the console.

public class Mammal : Animal
{

This line defines a new class called ‘Mammal’ that inherits from the ‘Animal’ class class by using colon – “:”.
Inheriting means ‘Mammal’ will have all the features of ‘Animal’ plus its own features.

public void Breathe()
    {

This line defines a method called ‘Breathe’ inside the ‘Mammal’ class.
The ‘Breathe’ method is specific to the ‘Mammal’ class.

Console.WriteLine(“Mammal is breathing.”);

This line prints the text “Mammal is breathing.” to the console.

public class Dog : Mammal
{

This line defines a new class called ‘Dog’ that inherits from the ‘Mammal’ class class by using colon – “:”.
Inheriting means ‘Dog’ will have all the features of ‘Mammal’ plus its own features.

public void Bark()
    {

This line defines a method called ‘Bark’ inside the ‘Dog’ class.
The ‘Bark’ method is specific to the ‘Dog’ class.

Console.WriteLine(“Dog is barking.”);

This line prints the text “Dog is barking.” to the console.

Course Video :
Course Video English :
Task:

1. Single Inheritance:
Create a base class Vehicle with a method Start that prints “Vehicle started”. Derive a class Car from Vehicle and add a method Drive that prints “Car is driving”. Create an object of Car in the Main method, call the Start and Drive methods.

2. Hierarchical Inheritance:
Create a base class Person with a method Walk that prints “Person is walking”. Derive two classes Student and Teacher from Person. Add a method Study in Student that prints “Student is studying” and a method Teach in Teacher that prints “Teacher is teaching”. Create objects of Student and Teacher in the Main method and call their respective methods.

3. Multilevel Inheritance:
Create a base class ElectronicDevice with a method TurnOn that prints “Device turned on”. Derive a class Computer from ElectronicDevice and add a method Boot that prints “Computer is booting”. Further derive a class Laptop from Computer and add a method Charge that prints “Laptop is charging”. Create an object of Laptop in the Main method, call all methods.

4. Inheritance with Constructors:
Create a base class Building with a property Address and a constructor to initialize this property. Derive a class House from Building and add a property NumberOfRooms. Add a constructor to House that calls the base class constructor. Create an object of House in the Main method and set all properties.

Frequently Asked Questions

Still have a question?

Let's talk

Inheritance is a feature in C# that allows a class (child or derived class) to inherit fields, methods, and properties from another class (parent or base class).

Inheritance promotes code reuse, enabling developers to build upon existing code rather than rewriting it. It also supports hierarchical classification and simplifies code maintenance.

  • Single Inheritance: A derived class inherits from one base class.
  • Multilevel Inheritance: A derived class inherits from another derived class.
  • Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
    Note: C# does not support multiple inheritance directly but uses interfaces to achieve similar functionality.

The base class is the parent class that provides common functionality, while the derived class is the child class that inherits and extends the base class’s functionality.

Yes, using the override keyword in the derived class. The method in the base class must be declared with the virtual keyword.

The base keyword is used to access methods, properties, or constructors of the base class from the derived class.

Constructors are not inherited in C#, but a derived class can call a base class constructor using the base keyword.

A class or method marked with the sealed keyword cannot be inherited or overridden further.

  • C# does not support multiple inheritance for classes but allows it for interfaces.
  • Overusing inheritance can lead to tightly coupled code.
  • Changes in the base class can impact all derived classes, potentially introducing bugs.

Single Inheritance in C# is when a class derives from one base class only, inheriting its properties and methods.

Example:

class Animal

{

    public void Eat()

    {

        Console.WriteLine(“Animal is eating.”);

    }

}

class Dog : Animal

{

    public void Bark()

    {

        Console.WriteLine(“Dog is barking.”);

    }

}

class Program

{

    static void Main()

    {

        Dog dog = new Dog();

        dog.Eat();

        dog.Bark();

    }

}

In this example, the Dog class inherits from the Animal class and gains access to the Eat() method.

Multilevel Inheritance in C# is when a class inherits from a base class, and another class inherits from that derived class.

Example:

class Animal

{

    public void Eat()

    {

        Console.WriteLine(“Animal is eating.”);

    }

}

class Dog : Animal

{

    public void Bark()

    {

        Console.WriteLine(“Dog is barking.”);

    }

}

class Puppy : Dog

{

    public void Play()

    {

        Console.WriteLine(“Puppy is playing.”);

    }

}

class Program

{

    static void Main()

    {

        Puppy puppy = new Puppy();

        puppy.Eat();

        puppy.Bark();

        puppy.Play();

    }

}

In this example, the Puppy class inherits from Dog, which in turn inherits from Animal, allowing the Puppy class to access methods from both classes.

Iqra Technology Academy offers detailed video tutorials in Hindi that explain the concept of inheritance in C#. Our tutorials cover single, multilevel, and hierarchical inheritance with practical examples to help you understand the topic thoroughly.

At Iqra Technology Academy, we offer a free online course in Hindi that covers inheritance in C#. The course includes clear explanations and practical examples to help you learn inheritance in C# from scratch.

Inheritance in C# is a fundamental object-oriented programming concept that allows a class (derived class) to inherit properties and methods from another class (base class). This enables code reuse and establishes a relationship between the base class and derived class.