1

I have two interface like this:

interface IA{
   void show();
}

interface IB{
   void show();
}

now i want to implement both interface method Explicitly into a class.

In C# i can do this by using "Explicit interface implementation method" like this:

interface IA
{
   void show();
}
interface IB
{
   void show();
}

class Derived : IA, IB
{
    void IA.show()
    {
        Console.WriteLine("Hello C#!");
    }
    void IB.show()
    {
        Console.WriteLine("Hello World !");
    }

}

Is there any way or any keyword for java to solve this problem?

Nasir Islam Sujan
  • 362
  • 1
  • 3
  • 20

2 Answers2

2

As long as both methods have the same return type, they can only be implemented as a single method within the class.

Otherwise, they cannot be implemented and a compile error is viewed.

There is no way in JAVA to explicitly implement each method per interface.

KAD
  • 10,972
  • 4
  • 31
  • 73
  • 1
    The OP doesn't ask how he could implement a single time the same method from two distinct interfaces. On the contrary, he wants to know in this case, how to give two implementations according to the real instance derives from the first or the second interface. Which is not allowed by Java. – davidxxx Feb 11 '17 at 09:40
  • That's what I answered in _"There is no way in JAVA to explicitly implement each method per interface."_ – KAD Feb 11 '17 at 09:41
  • Ah ok. it is not very clear as you refer to the return type requirement just before "Otherwise, they cannot be implemented and a compile error is viewed." and you chain with : "There is no way in JAVA to explicitly implement each method per interface." These are two different specificites. – davidxxx Feb 11 '17 at 09:51
-2

What you want to get is called multiple inheritance. In java Multiple Inheritance is not allowed so you can't do that in JAVA.

Refer below link for detail:

[http://www.geeksforgeeks.org/java-and-multiple-inheritance/][1]

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47