karansingh1487
karansingh1487

Reputation: 105

Implementing multiple inheritence using two interfaces having same methods

I understand the concept of multiple inheritance though i am trying to access the same method that were given in two interfaces . Example:-

interface Interface1{ 
       int show();
       void display();
}

interface Interface2 {
int show();
void display();
}

class Impl implements Interface1, Interface2 {
       // how to override show() and display() methods such that 
       // i could access both the interfaces
}

Upvotes: 0

Views: 3505

Answers (6)

Akash
Akash

Reputation: 11

You can implement 2 interfaces with same methods. No compilation error will occur. But when you do so

class A implements Interface1, Interface2
{
}

Then the similar methods of Interface will overridden by the Methods of Interface2. So in your class A there would only one method not two.

Please correct me if I'm wrong.

Upvotes: 1

CodeChimp
CodeChimp

Reputation: 8154

@RahulBorkar I do not believe that is correct. If you have two interfaces with the same signature, but different return types, you get the one based on the reference you are using.

For instance, if you create an object and put in in a Instance1 reference, you get the Instance1 method. If you take the same object, put it in an Instance2 reference, you will get the Instance2 method. This is how it seems to work for classes, at least. Its not based on the return type, but the calling type.

Upvotes: 0

kundan bora
kundan bora

Reputation: 3889

Interface only provide Contract or you can say restriction to implement some specific things. If two interface provide same Contract(As in your case) then Implementing class will implement only one contract(As there is no difference between two contract).

Upvotes: 0

John B
John B

Reputation: 32959

All an interface does is state that X method will exist in a class. If two interfaces define the same method and if a single class implements both interfaces, then the class will just have the one method. That method will satisfy the requirements of both interfaces. There is no way to define the same method signature twice in a single class in order to have a different implementation per interface.

In other words, in the following code, both calls to show execute the same method:

Impl impl = new Impl();
((Interface1) impl).show();
((Interface2) impl).show();

Upvotes: 0

Chandra Sekhar
Chandra Sekhar

Reputation: 19502

Interface1 i1 = new impl();
Interface2 i2 = new impl();
i1.show();
i2.show();

In both the cases, it seems you are calling two different methods of two different interfaces. But as interfaces methods does not have body parts, same method of Impl class will be executed.

So it should not matter through which reference you are calling these methods.

Upvotes: 0

Rahul Borkar
Rahul Borkar

Reputation: 2762

As interface doesn't have method definitions. it will not matter which interface's show method you are overriding.

Upvotes: 5

Related Questions