C# Program to Implement Multiple-Inheritance using Abstract Class and Interface
Last Updated :
28 Jan, 2022
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();
}
}
Outputprint1() called
print2() called
Similar Reads
C# Program to Demonstrate the Inheritance of Abstract Classes
Abstraction is the process to hide the internal details and show only the functionality. The abstract keyword is used before the class or method to declare the class or method as abstract. And inheritance is the object-oriented programming methodology by which one class is allowed to inherit the fea
2 min read
C# Program to Check a Specified Class is an Abstract Class or Not
Abstraction is the process to hide the internal details and showing only the functionality. The abstract keyword is used before the class or method to declare the class or method as abstract. In this article, we will learn how to check a specified class is an abstract class or not. To do this task w
2 min read
C# Program to Check a Specified Type is an Interface or not
The interface is just like a class, it can also have methods, properties, events, etc. as its members, but it only contains the declaration of the members and the implementation of these members will be given by the class that implements the interface implicitly or explicitly. We can check the speci
2 min read
C# Program to Check a Class is a Sub-Class of a Specified Class or Not
A class is a collection of methods, variables, and objects. A subclass is a class that is extended from the parent class. It should achieve all the properties from the parent class. Its syntax is similar to a class. Using: operator we can create the subclass. We can check the class is a subclass of
2 min read
C# Program to Demonstrate Abstract Class with Multiple-level Inheritance
Abstract Class is the way to achieve abstraction. It is a special class that never be instantiated directly. This class should contain at least one abstract method in it and mark by abstract keyword in the class definition. The main purpose of this class is to give a blueprint for derived classes an
3 min read
How to Implement Interfaces Using Abstract Class in C++
C++ doesn't have built-in functionality of interfaces like other programming languages such as Java. However, interfaces can be implemented in C++ using the abstract classes and pure virtual functions. In this article, we will learn how to implement interfaces using abstract classes in C++.Implement
4 min read
C# Program to Demonstrate Interface Implementation with Multi-level Inheritance
Multilevel Inheritance is the process of extending parent classes to child classes in a level. In this type of inheritance, a child class will inherit a parent class, and as well as the child class also act as the parent class to other class which is created. For example, three classes called P, Q,
3 min read
C# Program to Implement Multiple Interfaces in the Same Class
Like a class, Interface can have methods, properties, events, and indexers as its members. But interface will contain only the declaration of the members. The implementation of interfaceâs members will be given by the class that implements the interface implicitly or explicitly. C# allows that a sin
3 min read
Implement Interface using Abstract Class in Java
Interface contains only abstract methods that can't be instantiated and it is declared by keyword interface. A class that is declared with the abstract keyword is known as an abstract class in Java. This is a class that usually contains at least one abstract method which can't be instantiated and It
3 min read
C# Program to Inherit an Abstract Class and Interface in the Same Class
Abstract Class is the way to achieve abstraction. It is a special class that never be instantiated directly. This class should contain at least one abstract method in it and mark by abstract keyword in the class definition. The main purpose of this class is to give a blueprint for derived classes an
3 min read