Open In App

C# Program to Implement Multiple-Inheritance using Abstract Class and Interface

Improve
Improve
Like Article
Like
Save
Share
Report

Given multiple inheritance, now our task is to implement multiple inheritance with the help of abstract class and interface. So, before implementation first of all we learn the basic definition of multiple-inheritance, abstract class, and interface. 

Multiple inheritance: Multiple inheritance is a type of inheritance that is followed in object-oriented programming languages like C#, C++, etc. In this particular inheritance, a class inherits the properties of more than one parent class. For example, in the given figure Base class is inherited from two base classes, Parent 1 and Parent 2.

Fig: Base inheriting Parent 1 and Parent 2

Abstract Class: In order to understand abstract class, let us first understand what is abstraction in C#. Abstraction in C# is a process to represent the only necessary information to the outside world. In simple words, in this process, the background details remain hidden and only the functionality is visible to the outside world. 

Now we are aware of abstraction, let’s come back to the topic abstract class. Abstract class in C# is a path to achieve abstraction in C#. An abstract class cannot be instantiated directly. This class must have at least one abstract method. The purpose of using an abstract class in a program is to provide a blueprint for the derived class and set some parameters that the derived class must implement.

Interface: An interface is similar to a class in C#. But unlike a class, an interface must have only the declarations (or prototype) of its members.

  • Interfaces signify that what a class must do and how.
  • There can’t be any private member in an interface.
  • All the members of Interface are public and abstract (By default).
  • To define an interface we must use the keyword ‘interface‘.
  • Fields cannot be provided to an interface because they represent a particular implementation of data.
  • With the help of interfaces, multiple inheritance is possible.

Now we learn how to implement multiple-inheritance using abstract class and interface with the help of an example:

Example: In this program, we created an abstract class myAbstractClass and an interface myInterfaceClass, and two-parent classes myClass1, myClass2. Where we are overriding the abstract method print1() in myClass1 class and implementing print2() of interface myInterfaceClass into myClass2 class. After that, we created a child class myClass3, now we inherited the myClass1 class and myInterfaceClass interface. In the myClass3 class, we created the object of myClass1 and myClass2 class and here we defined two more methods print1(), print2(), and called print1() of myClass1 class inside print1() method of myClass3, and called print2() of myClass2 class inside print2() method of myClass3. The Main() method is the origin of any program. Now we created the object myObject of myClass3 class and called print1() and print2() that will display the message on the console screen as an output.

C#




// C# program to implement multiple inheritance
// using abstract class and interface
using System;
 
// Defining an abstract class
abstract class myAbstractClass
{
     
    // Method Declaration
    public abstract void print1();
}
 
// Parent class
class myClass1 : myAbstractClass
{
     
    // Overriding print1() method of myAbstractClass
    public override void print1()
    {
        Console.WriteLine("print1() called");
    }
}
 
interface myInterfaceClass
{
     
    // Method Declaration
    void print2();
}
 
// Another Parent class
class myClass2 : myInterfaceClass
{
    public void print2()
    {
        Console.WriteLine("print2() called");
    }
}
 
// Base class
class myClass3 : myClass1, myInterfaceClass
{
    myClass1 S1 = new myClass1();
    myClass2 S2 = new myClass2();
 
    // Overriding print1() method of myAbstractClass
    public override void print1()
    {
        S1.print1();
    }
     
    public void print2()
    {
        S2.print2();
    }
}
 
// Driver code
class GFG{
 
static public void Main()
{
     
    // Instantiate an object of myClass3
      myClass3 myObject = new myClass3();
 
    myObject.print1();
    myObject.print2();
}
}


Output

print1() called
print2() called


Last Updated : 28 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads