Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarify the concept of Multiple inheritance: Can a diamond structure issue occur?

Suppose you have two interfaces I1 and I2 both the interfaces have the same method

1)public int add(int a) throws exception e1    //(Interface 1)

2)public int add(int a) throws exception e2    //(Interface 2)

now a class implements both I1 and I2.

@override
public int add(int a) exception e1
{
//adding operation
}

Note: I understand that if you implement multiple interfaces which have same method then only one implementation will be there in the implementing class.

  1. Does the compiler infer whether this method is from I1 or I2 based on the throws clause? Is method signature clarification required? (I know that signature include name and parameters passed.)

  2. If we have implemented both the interfaces and then we are throwing only e1 will the compiler allow this since the implementing class doesn't really implement both interfaces? will it cause diamond structure problem?

  3. Do we need to implement the same method with throws clause e1 and 2nd time e2? When I tried this the compiler told me I had duplicate methods.

  4. If we implement the I1 first then it says that it is I1's method and if we write the I2 first then it says its I2's method. Explain why this is so.

like image 691
SHK_theTechnoGuru Avatar asked Feb 14 '23 17:02

SHK_theTechnoGuru


2 Answers

An interface defines a contract. If an interface defines a method

void foo() throws SomeException;

then it says: all implementing classes must have a method foo, returning void, not taking any argument, and which is allowed to throw SomeException but no other type of checkd exception.

The implementing method may of course choose to not throw any exception at all, since it would not violate the contract of the interface method. throws means: this method might throw this exception in some circumstances.

So, a method declared as

public void foo();

in an implementing class is valid for the interface defined above.

So, in your example,

public int add(int a) throws Exception1

is a valid method declaration to override

int add(int a) throws Exception1

but is not valid for

int add(int a) throws Exception2

(unless of course Exception1 is a subclass of Exception2).

The only way, if there is no inheritance between Excption1 and Exception2, to implement both interfaces is to have a method that doesn't throw any exception. That is the only possibility to fullfill the two contracts.

like image 131
JB Nizet Avatar answered Feb 17 '23 09:02

JB Nizet


With regard to overriding, only the parameter types of methods are considered (8.4.2); return types and throw types are checked later in a separate step (8.4.8.3).

It is possible for a class to inherit multiple methods with override-equivalent signatures. (8.4.8.4) There is no problem there. In your case, two methods are inherited, then both are overridden by one method.

My IDE IntelliJ correctly reports that the method overrides two methods in two super interfaces.

In Java 7 and earlier, there is no diamond problem. However since Java 8, interface methods can have implementations! Therefore the diamond problem can occur when a class inherits two methods from two interfaces. Javac may reject the code if such ambiguity exists. (There are ways to disambiguate).

like image 38
ZhongYu Avatar answered Feb 17 '23 09:02

ZhongYu