0

Just a small confusion. Java does not allow multiple inheritance. Then how can an interface extend more than one interfaces? Can we call it inheritance?

Leo
  • 5,017
  • 6
  • 32
  • 55
  • Hmm...maybe with Java 8 where you can provide default methods for interfaces. Because a class can implement more than one interface we have something like multiple inheritance. – sk2212 Nov 04 '14 at 09:39
  • Implementing multiple Interfaces is **polymorphism** not **inheritance**. – Dmytro Nov 04 '14 at 09:43
  • @Dmytro That's not correct. A type inherits all public members of its supertypes. That includes all method definitions, nested types, and `static` members of class and interface types alike. – Marko Topolnik Nov 04 '14 at 09:45
  • For the record, I find [this answer](http://stackoverflow.com/a/26339439/1103872) to offer the best explanation as to the key difference between inheriting from multiple classes vs. interfaces. – Marko Topolnik Nov 04 '14 at 09:53
  • @MarkoTopolnik So, now you realize difference between implementation and inheritance? – Dmytro Nov 04 '14 at 09:57
  • @Dmytro Problem is, you don't seem to be making the proper connections between the concepts you are wielding. A type inherits both concrete and abstract methods from its parent types. – Marko Topolnik Nov 04 '14 at 10:01
  • @MarkoTopolnik yes, that is true, but it is too general thus carries only very limited payload. – Dmytro Nov 04 '14 at 10:10
  • @Dmytro I am asking about extending interfaces by another interface...not implementing interfaces. – Leo Nov 04 '14 at 10:19
  • @Leo Thus it is multiple inheritance which is possible for interfaces as an exclusion in Java. – Dmytro Nov 04 '14 at 10:21
  • Leo, you are simply wrong in thinking that Java does not allow multiple inheritance. It definitely does allow it, and especially so as of version 8, where you can even multiple-inherit *behavior*. As explained in the answer I indicated above, the only real trouble happens with multiple inheritance of *state*, which is the only thing Java prevents. – Marko Topolnik Nov 04 '14 at 10:28

1 Answers1

0

An interface extending multiple interfaces does not constitute multiple inheritance. Since you are not inheriting any functionality.

public interface A {
    public void doSomething();
}

public interface B {
    public void doSomething();
}

public interface C extends A, B {

}

Is absolutely fine, since you end up with a single interface with (effectively) one method:

public interface C extends A, B {
    public void doSomething();    
}
StuPointerException
  • 7,117
  • 5
  • 29
  • 54
  • Thanks but suppose interface A and B contains two different methods(say m1 and m2). So as C is extending both interfaces it will also have both m1 and m2. Hence, though C is not extending any functionality still it acquires the behavior of A and B. Then how would justify this. – Leo Nov 04 '14 at 10:16
  • @Leo: It's justified because it doesn't cause the traditional problems associated with multiple inheritance. You're extending the contract for an interface, not inheriting behaviour from a class. – StuPointerException Nov 04 '14 at 10:58