1

One of the answer I found was "multiple inheritance can lead to conflicts if two methods/variable belonging to two different classes have the same name". But I think there can be conflict while implementing multiple interfaces as well. For eg consider a class implementing two interfaces having same variable name(variables can be defined in an interface and they are final by default) declared inside them

interface Ainterface {

    public final static int i=10;

}


class InterfaceCheck implements Ainterface {
        public static void main(String[] args) {        
        System.out.println(i);
    }

}

The above code works perfectly fine

interface Ainterface {

    public final static int i=10;

}

interface Binterface {

    public static final int i=20;

}

class InterfaceCheck implements Ainterface,Binterface {
        public static void main(String[] args) {        
        System.out.println(i);
    }

}

As per the sources on the internet "implementing multiple interfaces can never lead to conflict" But the above code produces an error. So this is the conflict I am talking about.

  • Things get complicated when the same base class is inherited multiple times via different inheritance chains. This is something the designers of Java did not want to get into. Therefore the restriction to inherit just one class. Most interesting applications of multiple inheritance can be obtained from interfaces. – Henry Jul 02 '17 at 05:32
  • @chirag As I have mentioned in the posted answer, the defined methods or fields will show conflict. Since the fields are static, you can safely use Ainterface.i or Binterface.i in println(). Were it a default method of a functional interface, it would show compilation error in multiply inheriting the interfaces and will not allow the two interfaces to be multiply inherited. – hi.nitish Jul 02 '17 at 09:28
  • @ChiragJain It is good you are analysing all nitty-gritty, I remember I thought the same when I read java by Khalid. Even here, if you access the common field without using InterfaceName.i, you get compilation error! If you do not access the ambiguous field, you are fine on compile and run time.Please upvote if you liked the ans. The same issue also comes when you use Outer-Inner class. – hi.nitish Jul 02 '17 at 11:03
  • Yes, this example errors, as you seem to expect. There is indeed a conflict. But no question. What's your question? – Patrick Roberts Jul 02 '17 at 11:29
  • @Nitish I can't upvote due to low reputation. – Chirag Jain Jul 02 '17 at 13:07
  • @patrick the whole reason why Java does not allow multiple inheritance and allows multiple interface is that "extending multiple class leads to conflict while implementing multiple interfaces does not". I just wanted to know how is it preventing the conflict – Chirag Jain Jul 02 '17 at 13:12
  • @ChiragJain by not compiling when there's a conflict... – Patrick Roberts Jul 02 '17 at 15:28

3 Answers3

6

variables can be defined in an interface and they are final by default

*Variables in Java interfaces are public, static and final by default. This is because interfaces are not meant to dictate implementation, only behavior. That means variables in an interface are effectively constants.

Static variables are not inherited. So there is no possibility of conflict.

havanagrawal
  • 1,039
  • 6
  • 12
  • I don't see the problem. The code you posted with 2 interfaces fails to compile. So it is impossible to have the conflict you're talking about. – havanagrawal Jul 02 '17 at 11:08
  • The same error could have been thrown in multiple inheritance as well. Why didn't they allow multiple inheritance then? Anyways, I got the answer. Thanx – Chirag Jain Jul 02 '17 at 13:19
  • Everything would be so easier, if we could end the discussion this way. Atleast don't close the question. What if someone has an answer ?. My question is no more unclear as you've marked – Chirag Jain Jul 02 '17 at 17:03
0

It will implement the objects from both the interfaces . So if both the interfaces have same variable, it will implement both as a single variable. Same applies to methods as well.

Praveen E
  • 926
  • 8
  • 14
0

multiple inheritance in classes is not allowed because of the diamond problem. Simple answer is how would super.commonMethodInBothClasses() work? How would jvm know which method definition is to be used in child class in super.method call? This causes ambiguity. In java8, you may find one surprise, we are allowed to define methods in interfaces, called default methods. But multiple inheritance is still allowed in interfaces (functional interfaces), but here it is checked at compile time that the interfaces must not have a method in common if it has a definition in interfaces. If two methods are declared but not defined with same signature, it is allowed as the conflict is between the definitions which is not there in both the interfaces.

Interface1{
  public static final int a= 10;
default void method1(){
// definition
}
void method2DeclaredOnly();
}

Interface2{
 public static final int a = 20;
default void method3(){
// definition
}
void method2DeclaredOnly();
}

class MyClass implements Interface2, Interface1{

void method2DeclaredOnly(){
System.out.println(Interface1.a); // static members can be referred. directly. Classname.field 
// defined here allowed.
}

However, it is mandatory to define the default method, if at all, you are implementing both interfaces, Otherwise it gives compilation error as talked earlier. Now here the tricky part is, the common default method is called using IA.super.abc();

    interface IA{
        default void abc(){
            //..
        }
    }

    interface IB{
default void abc(){
            //..
        }
    }

    class A implements IA, IB
    {
        public void abc(){
            IA.super.abc(); //..

                }
    }
hi.nitish
  • 2,732
  • 2
  • 14
  • 21