C# program to implement multiple-inheritance using abstract class and interface

Here, we are going to learn how to implement multiple-inheritance using abstract class and interface in C#?
Submitted by Nidhi, on October 26, 2020

Here, we will create an abstract class and an interface and then implement multiple-inheritance by implementing methods.

Program:

The source code to implement multiple-inheritance using abstract class and interface is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to implement multiple-inheritance 
//using abstract class and interface

using System;

abstract class Abs1
{
    //Method Declaration
    public abstract void Method1();
}

//Parent class 1
class Sample1 : Abs1
{
    public override void Method1()
    {
        Console.WriteLine("Method1() called");
    }
}

interface MyInf2
{
    //Method Declaration
    void Method2();
}

//Parent class 2
class Sample2 : MyInf2
{
    public void Method2()
    {
        Console.WriteLine("Method2() called");
    }
}

class Sample3 : Sample1, MyInf2
{
    Sample1 S1 = new Sample1();
    Sample2 S2 = new Sample2();

    public void Method1()
    {
        S1.Method1();
    }

    public void Method2()
    {
        S2.Method2();
    }
}

class Program
{
    public static void Main(String[] args)
    {
        Sample3 S = new Sample3();

        S.Method1();
        S.Method2();
    }
}

Output:

Method1() called
Method2() called
Press any key to continue . . .

Explanation:

Here, we created an abstract class Abs and an interfaces MyInf2, and two-parent classes Sample1, Sample2. Here we override abstract method Method1() in Sample1 class and implement Method2() of interface MyInf2 into Sample2 class. After that, we created a child class Sample3, here we inherited the Sample1 class and MyInf2 interface.

In the Sample3 class, we created the object of Sample1 and Sample2 class and here we defined two more methods Method1(), Method2(), and called Method1 of Sample1 class inside Method1() method of Sample3, and called Method2 of Sample2 class inside Method2() method of Sample3.

Now look the Program class that contains the Main() method. The Main() method is the entry point for the program. Here we created the object S of Sample3 class and called Method1() and Method2() that will print the corresponding message on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.