Q:

C# program to implement multiple-inheritance using the interface

0

C# program to implement multiple-inheritance using the interface

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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

//C# program to implement multiple-inheritance 
//using the interface

using System;

interface MyInf1
{
    //Method Declaration
    void Method1();
}

//Parent class 1
class Sample1 : MyInf1
{
    public 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 : MyInf1,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 two interfaces MyInf1MyInf2, and two-parent classes Sample1Sample2. Here, we implemented both interfaces into Sample1 and Sample2 classes. After that, we created a child class Sample3, here we inherited the interfaces MyInf1MyInf2.

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to implement hierarchical inheritance u... >>
<< C# program to implement an interface in a structur...