How to Inherit From Multiple Classes in C#

Saad Aslam Feb 02, 2024
How to Inherit From Multiple Classes in C#

A class or object can inherit features and characteristics from one or more parent objects or classes in the OOP language. When a subclass requires access to any or all of a parent class’s properties, inheritance is utilized.

It’s also handy when a child’s class needs to merge many base class constructors. In this tutorial, we’ll tackle how to implement multiple inheritances in C#.

Implement Multiple Inheritance in C#

In the C# language, the most well-known and straightforward technique is the utilization of interfaces. Here we have an example in which we’ll implement inheritance from multiple classes to calculate BMI.

We’ll learn this step by step. Firstly, we need to import the System library to access the methods used in C#.

using System;

We’ll create a class named Attributes with the setter methods, which we shall use to retrieve values from the other classes. Declare two double-type variables inside it named weight and height.

We’ll keep them protected, meaning they won’t be available to anybody outside our class. Next, create a method setWeight() with a double-type argument w that assigns the provided value to weight, and create the setHeight() method that will set the value of height in the same way as this one.

class Attributes {
  protected double weight;
  protected double height;
  public void setWeight(double w) {
    weight = w;
  }

  public void setHeight(double h) {
    height = h;
  }
}

Then, create an interface RetrieveAge containing a double-type method retrieveAge that takes an argument age of double data-type.

public interface RetrieveAge {
  double retrieveAge(double age);
}

Create a BMI class that will inherit two classes, Attributes and RetrieveAge. We’ll create 4 methods to retrieve values from their parent classes inside this class.

The first method is a double data-type called retrieveBMI(), which gets weight and height from the parent class Attributes, divides weight by the square of height, and returns the result. The next two methods, retrieveHeight() and retrieveWeight(), will fetch the height and weight values and return them.

The last method, retrieveAge(), extends the method from RetrieveAge that takes an argument age of double-type and returns it.

class BMI : Attributes, RetrieveAge {
  public double retrieveBMI() {
    return (weight / (height * height));
  }

  public double retrieveHeight() {
    return height;
  }

  public double retrieveWeight() {
    return weight;
  }

  public double retrieveAge(double age) {
    return age;
  }
}

We create our final class that will have the Main() function. Inside the main function, create a BMI class instance.

Use the setHeight() and setWeight() methods to assign height and weight. Finally, we need to output height, weight, calculated BMI, and age.

Full Code:

using System;

class Attributes {
  protected double weight;
  protected double height;
  public void setWeight(double w) {
    weight = w;
  }

  public void setHeight(double h) {
    height = h;
  }
}

public interface RetrieveAge {
  double retrieveAge(double age);
}

class BMI : Attributes, RetrieveAge {
  public double retrieveBMI() {
    return (weight / (height * height));
  }

  public double retrieveHeight() {
    return height;
  }

  public double retrieveWeight() {
    return weight;
  }

  public double retrieveAge(double age) {
    return age;
  }
}

class TotalBMI {
  static void Main() {
    BMI bmi = new BMI();
    bmi.setWeight(80);
    bmi.setHeight(2.07);

    Console.WriteLine("Your Height: {0} m", bmi.retrieveHeight());
    Console.WriteLine("Your Weight: {0} kg", bmi.retrieveWeight());
    Console.WriteLine("BMI: {0}", bmi.retrieveBMI());
    Console.WriteLine("Age: {0}", bmi.retrieveAge(40));
  }
}

Output:

Your Height: 2.07 m
Your Weight: 80 kg
BMI: 18.6702140073281
Age: 40
Author: Saad Aslam
Saad Aslam avatar Saad Aslam avatar

I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.

LinkedIn

Related Article - Csharp Class