C# Program to Implement Multiple-Inheritance using Abstract Class and Interface
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.
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.
print1() called print2() called
Please Login to comment...