162

Can an interface extend multiple interfaces in Java? This code appears valid in my IDE and it does compile:

interface Foo extends Runnable, Set, Comparator<String> { }

but I had heard that multiple inheritance was not allowed in Java. Why does there appear to be an exception for interfaces?

Michael
  • 41,989
  • 11
  • 82
  • 128
Prateek
  • 6,785
  • 2
  • 24
  • 37
  • 3
    Which articles say it's not possible? They're either wrong, or you misunderstood them and they're talking about classes rather than interfaces. –  Oct 23 '13 at 15:47
  • 7
    This question appears to be off-topic because it could easily be answered by self-research – JoseK Oct 23 '13 at 15:49
  • ... or simply by reading the articles you found *more carefully*. – Stephen C Oct 23 '13 at 15:52
  • 4
    @StephenC If he did find contradicting articles, he should post them. That way people know not to read those articles... – But I'm Not A Wrapper Class Oct 23 '13 at 15:53
  • @MohammadS - that is true, but I doubt that it is the case. This is really basic stuff, and I'd bet the problem is in the reading, not the writing ... – Stephen C Oct 24 '13 at 04:43
  • 12
    The answer to this question saved me time. I argue that it has value as a valid question. – xdhmoore Mar 30 '15 at 17:09
  • yes, extends and implement are different from each. – Thusitha Wickramasinghe Nov 09 '15 at 10:51
  • 7
    helpful question, I did this expecting my IDE to throw some warnings, was amazed to see none. So I unknowingly typed in google the same question as the op's which led me to this page with some answers/confirmation – Arthur Jun 23 '16 at 02:37
  • Java does not allow multiplle inheritance of *state* (classes), but it allows multiple inheritance of *behaviour* (interface). – Siddhesh Rane Sep 14 '20 at 17:38

7 Answers7

208

Yes, you can do it. An interface can extend multiple interfaces, as shown here:

interface Maininterface extends inter1, inter2, inter3 {  
  // methods
}

A single class can also implement multiple interfaces. What if two interfaces have a method defining the same name and signature?

There is a tricky point:

interface A {
    void test();
}

interface B {
    void test();
}

class C implements A, B {

    @Override
    public void test() {

    }     

}

Then single implementation works for both :).

Read my complete post here:

http://codeinventions.blogspot.com/2014/07/can-interface-extend-multiple.html

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • @RahulBhadana Abstract method should override right ? – Suresh Atta Jul 28 '14 at 08:02
  • 18
    What if you have interface A with `void test()` and interface B with `boolean test()`? (Is this a cousin of the [diamond problem](https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem)?) __Tried it and the sensible thing happens: not allowed if the return type is different. – Daniel Jan 06 '15 at 17:38
  • @sureshatta - so what happens? does it **call both** ?? – Fattie Dec 08 '16 at 14:51
  • 7
    A class doesn't allow 2 methods same signature with different return type. http://stackoverflow.com/questions/16149285/does-a-methods-signature-in-java-include-its-return-type – ninhjs.dev Mar 26 '17 at 11:59
89

An interface can extend multiple interfaces.

A class can implement multiple interfaces.

However, a class can only extend a single class.

Careful how you use the words extends and implements when talking about interface and class.

20

From the Oracle documentation page about multiple inheritance type,we can find the accurate answer here. Here we should first know the type of multiple inheritance in java:-

  1. Multiple inheritance of state.
  2. Multiple inheritance of implementation.
  3. Multiple inheritance of type.

Java "doesn't support the multiple inheritance of state, but it support multiple inheritance of implementation with default methods since java 8 release and multiple inheritance of type with interfaces.

Then here the question arises for "diamond problem" and how Java deal with that:-

  1. In case of multiple inheritance of implementation java compiler gives compilation error and asks the user to fix it by specifying the interface name. Example here:-

                interface A {
                    void method();
                }
    
                interface B extends A {
                    @Override
                    default void method() {
                        System.out.println("B");
                    }
                }
    
                interface C extends A { 
                    @Override
                    default void method() {
                        System.out.println("C");
                    }
                }
    
                interface D extends B, C {
    
                }
    

So here we will get error as:- interface D inherits unrelated defaults for method() from types B and C interface D extends B, C

You can fix it like:-

interface D extends B, C {
                @Override
                default void method() {
                    B.super.method();
                }
            }
  1. In multiple inheritance of type java allows it because interface doesn't contain mutable fields and only one implementation will belong to the class so java doesn't give any issue and it allows you to do so.

In Conclusion we can say that java doesn't support multiple inheritance of state but it does support multiple inheritance of implementation and multiple inheritance of type.

shankar upadhyay
  • 883
  • 12
  • 18
  • 2
    I think this is a more suitable answer and clarifies more to the question. +1 – Ketan Joshi Nov 20 '19 at 17:36
  • Thank you @KetanJoshi this motivates a lot to carry on :) – shankar upadhyay Nov 21 '19 at 10:20
  • @Danger here the focus is not on defining a method inside interface, obviously from Java 8 we can define it. Hope i have answered you. Thanks Shankar – shankar upadhyay May 24 '21 at 13:18
  • Hi @shankarupadhyay, I'm just saying that if you are defining interface then do not put method implementation inside an interface, just declare the methods , or use class. As developers we should not do these mistakes, even if the focus is on something else. – Danger May 26 '21 at 07:40
11

Can an interface extend multiple interfaces in java?

Answer is: Yes.

According to JLS

An interface may be declared to be a direct extension of one or more other interfaces, meaning that it implicitly specifies all the member types, abstract methods, and constants of the interfaces it extends, except for any member types and constants that it may hide.

Masudul
  • 21,823
  • 5
  • 43
  • 58
4

You can extend multiple Interfaces but you cannot extend multiple classes.

The reason that it is not possible in Java to extending multiple classes, is the bad experience from C++ where this is possible.

The alternative for multipe inheritance is that a class can implement multiple interfaces (or an Interface can extend multiple Interfaces)

AlexWien
  • 28,470
  • 6
  • 53
  • 83
2

I think your confusion lies with multiple inheritance, in which it is bad practise to do so and in Java this is also not possible. However, implementing multiple interfaces is allowed in Java and it is also safe.

Cem Sultan
  • 354
  • 2
  • 12
2

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.

for example, take a look here: http://www.tutorialspoint.com/java/java_interfaces.htm

mmdc
  • 1,677
  • 3
  • 20
  • 32