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.

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.