C# Inheritance

HTML
CSS
Bootstrap
JavaScript
C#
SQL
Salesforce Admin
Exercise
Study Material

Inheritance in C#

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows classes to inherit attributes and behaviors from other classes, establishing a hierarchical relationship. In C#, inheritance enables the creation of a base class (or superclass) that defines common characteristics shared by multiple derived classes (or subclasses). This promotes code reusability, encapsulation, and the implementation of the relationship, where a subclass is a specialized version of its superclass.

 

In object-oriented programming (OOP), there are several types of inheritance that define relationships between classes. Each type of inheritance serves different purposes and facilitates specific modeling scenarios. Here are the common types of inheritance along with basic examples:

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.”);

    }

}

Hierarchy inheritance :

public interface IShape

{

    void Draw();

}

 

public abstract class Shape

{

    public abstract double CalculateArea();

}

 

public class Circle : Shape, IShape

{

    public override double CalculateArea()

    {

        return Math.PI * Radius * Radius;

    }

 

    public void Draw()

    {

        Console.WriteLine(“Drawing Circle.”);

    }

 

    public double Radius { get; set; }

}

Multilevel inheritance :

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.”);

    }

}

Hybrid inheritance:

public interface IShape

{

    void Draw();

}

 

public abstract class Shape

{

    public abstract double CalculateArea();

}

 

public class Circle : Shape, IShape

{

    public override double CalculateArea()

    {

        return Math.PI * Radius * Radius;

    }

 

    public void Draw()

    {

        Console.WriteLine(“Drawing Circle.”);

    }

 

    public double Radius { get; set; }

}

Practices Tasks

1. Bank Account Inheritance:

For example:  Create a base class BankAccount with properties AccountNumber and Balance, and create a constructor for this class. Include virtual methods named Deposit and Withdraw for adding and subtracting funds respectively. Then, create derived classes SavingsAccount and CheckingAccount with their own constructors that initialize them using the base class constructor. In the Main method, create instances of SavingsAccount and CheckingAccount with specified values. Invoke the Deposit() and Withdraw() methods on these accounts to perform transactions, passing appropriate values. Finally, output like below.

2. Mobile Phone Inheritance:

For example: Create a base class Mobilephone with properties brand and model and create a constructor for this class. Then, create derived classes smartphone with properties operating system and featurephone  with properties keyboards with their own constructors that initialize them using the base class constructor. In the Main method, create instances of smartphone and featuresphone with specified values. Finally, output like below.

3. Employee inheritance:

Create a base class Employee with properties Name, EmployeeId, and Department. Implement a virtual method DisplayInfo and a constructor for this class. Then, create a derived class Manager with an additional property TeamSize, along with its own constructor that initializes properties using the base class constructor. Override the DisplayInfo method in the Manager class. Include a method HoldMeeting specific to the Manager class for conducting meetings.