Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm coming out of an interview just now and the interviewer asked me if a Java interface can "extend" more than one interfaces. I was thinking multiple inheritance is disallowed in java so got that one wrong!

Turns out that you can extend from multiple interfaces in Java. So now my question:

Is extending multiple interfaces considered bad practice? What are the disadvantages of it?


interface A {
    public void doA();

interface B {
    public void doB();

interface C extends A, B {
    public void doC();

migration rejected from stackoverflow.com Nov 27 '13 at 23:11

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as primarily opinion-based by ChrisF Nov 27 '13 at 23:11

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

It's a good practice. My job here is done. To the batcave!! ha-ha!! –  Asier Aranbarri May 15 '13 at 13:13
have a look here, answer 1 is good, and I hunger for pizza now... stackoverflow.com/questions/3556652/… –  Najzero May 15 '13 at 13:14
@Justin: It Neither Good nor Bad..! –  Senthil Prabhu May 15 '13 at 13:15
This is tagged as Java but applies equally to most OO languages. –  Mauro May 15 '13 at 13:30

1 Answer 1

I'd like to point out something that bit me in the behind, coming from C++ where you can easily inherit many implementations too.

Having a "wide" interface with many methods means that you'll have to implement a lot of methods in your concrete classes and you can't share these easily across implementations.

For instance:

interface Herbivore {
    void munch(Vegetable v);

interface Carnivore {
    void devour(Prey p);

interface AllEater : public Herbivore, Carnivore { };

class Fox implements AllEater {

class Bear implements AllEater {

In this example, Fox and Bear cannot share a common base implementation for both it's interface methods munch and devour.

If the base implementations look like this, we'd maybe want to use them for Fox and Bear:

class ForestHerbivore implements Herbivore
    void munch(Vegetable v) { ... }

class ForestCarnivore implements Carnivore
    void devour(Prey p) { ... }

But we can't inherit both of these. The base implementations need to be member variables in the class and methods defined can forward to that. I.e:

class Fox implements AllEater {
    private ForestHerbivore m_herbivore;
    private ForestCarnivore m_carnivore;

    void munch(Vegetable v) { m_herbivore.munch(v); }
    void devour(Prey p) { m_carnivore.devour(p); }

This gets unwieldy if interfaces grow (i.e. more than 5-10 methods...)

A better approach is to define an interface as an aggregation of interfaces:

interface AllEater {
    Herbivore asHerbivore();
    Carnivore asCarnivore();

This means that Fox and Bear only has to implement these two methods, and the interfaces and base classes can grow independetly of the aggregate AllEater interface that concerns the implementing classes.

Less coupling this way, if it works for your app.