Sponsored Ad

Saturday, May 12, 2012

Interview Questions: Implement Same Name Method of two Interfaces in a Class using C#

This is most famous question in OOPs programming. if you have two interfaces and driving a class and these two interfaces have the same name method then how will you implement those same name methods.

The below program is an example of the above problem.

Here we have two interfaces Sparrow and Parrot, both have same method to print the BirdType.

So to implement the BirdType method in class Bird, use the interface name along with interface method name. This will fully qualify the method name. Program will execute without any error.

The example of the program is given below.

Interview Questions: Implement Same Name Method of two Interfaces in a Class using C#

using System;

namespace CSharpInterview
{
    interface Sparrow
    {
        void BirdType();
    }

    interface Parrot
    {
        void BirdType();
    }

    class Bird : Parrot, Sparrow
    {
        void Parrot.BirdType()
        {
            Console.WriteLine("Bird is Parrot.");
        }

        void Sparrow.BirdType()
        {
            Console.WriteLine("Bird is Sparrow.");
        }
    }

    class Programs
    {       
        static void Main(string[] args)
            {

                Parrot ObjParrot = new Bird();
                ObjParrot.BirdType();

                Sparrow ObjSparrow = new Bird();
                ObjSparrow.BirdType();
                Console.ReadLine();
            }
    }   
}

No comments:

Post a Comment

Sponsored Ad

Followers

Follow Us