C# program to demonstrate the inheritance of interfaces

Here, we are going to demonstrate the inheritance of interfaces in C#?
Submitted by Nidhi, on October 14, 2020

Here, we will implement the inheritance of interfaces, here we will inherit one interface into another interface.

Program:

The source code to demonstrate the inheritance of interfaces is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate the inheritance of interfaces

using System;

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

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

class Sample : MyInf2
{
    //Method definition
    void MyInf1.Method1()
    {
        Console.WriteLine("Method1() called");
    }

    void MyInf2.Method2()
    {
        Console.WriteLine("Method2() called");
    }

    
}

class Program
{
    public static void Main(String[] args)
    {
        MyInf1 M1;
        MyInf2 M2;

        M1 = new Sample();
        M2 = new Sample();

        M1.Method1();
        M2.Method2();
    }
}

Output:

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

Explanation:

Here, we created two interfaces MyInf1 and MyInf2. Here, we inherited the interface MyInf1 into MyInf2. Then implemented the MyInf2 interface in the class Sample. Here we implemented the methods of both interfaces.

Now look to the Program class, It contains the Main() method, the Main() method is the entry point for the program. Here we created the references of both interfaces that are initialized with the object of Sample class and then called the Method1() and Method2() that will print the appropriate 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.