81

I know that multiple inheritances between Interfaces is possible, e.g.:

public interface C extends A,B {...} //Where A, B and C are Interfaces

But is it possible to have a regular Class inherit from multiple Interfaces like this:

public class A implements C,D {...} //Where A is a Class and C and D are interfaces
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Joshua
  • 999
  • 1
  • 6
  • 8
  • 2
    That was the first thing I was going to do after trying google, but I found this question: http://stackoverflow.com/questions/13389384/multiple-inheritance-on-java-interfaces which allowed multiple inheritance from classes to compile which I know shouldn't be possible in Java. I then figured I could also get a "false" result so I decided to ask here. – Joshua Jan 21 '14 at 16:27
  • 1
    That link doesn't show multiple inheritance from classes anywhere as far as I can tell. – Sotirios Delimanolis Jan 21 '14 at 16:28
  • 3
    That's what I originally thought but I got confused since "extends" was used rather than "implements" – Joshua Jan 21 '14 at 16:29
  • 3
    The downvotes are for lack of demonstrating research. You may have done the research, but you didn't show it. – Sotirios Delimanolis Jan 21 '14 at 16:33

8 Answers8

159

A Java class can only extend one parent class. Multiple inheritance (extends) is not allowed. Interfaces are not classes, however, and a class can implement more than one interface.

The parent interfaces are declared in a comma-separated list, after the implements keyword.

In conclusion, yes, it is possible to do:

public class A implements C,D {...}
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • 1
    Thanks for the answer, I'm wondering how to use the "this" to refer to a certain interface in this case. – Rami Alloush Aug 07 '18 at 18:29
  • But why is it "ok" to do so. As i understand multiple inheritance is not allowed in java because both parent classes might have a method with the same signature. So while two different interfaces might have a method with same signature - why is it allowed to implemnt more than 1 interface? – Daniel Oct 03 '20 at 08:15
  • 1
    @Daniel Because if you have two concrete implementations of the same method, there is no good way to determine which one to use. In the case of implementing interfaces, there's no concrete implementation in _either_ of them, so there is no confusion. Your implementing class's method is the one being used. – Dave Lugg Aug 27 '21 at 17:52
11

In a word - yes. Actually, many classes in the JDK implement multiple interfaces. E.g., ArrayList implements List, RandomAccess, Cloneable, and Serializable.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
11

public class A implements C,D {...} valid

this is the way to implement multiple inheritence in java

java seeker
  • 1,246
  • 10
  • 13
10

Yes, a class can implement multiple interfaces. Each interface provides contract for some sort of behavior. I am attaching a detailed class diagram and shell interfaces and classes.

Ceremonial example:

enter image description here

public interface Mammal {
    void move();
    boolean possessIntelligence();
}
public interface Animal extends Mammal {
    void liveInJungle();
}

public interface Human extends Mammal, TwoLeggedMammal, Omnivore, Hunter {
    void liveInCivilization();
}
public interface Carnivore {
    void eatMeat();
}
public interface Herbivore {
    void eatPlant();
}
public interface Omnivore extends Carnivore, Herbivore {
    void eatBothMeatAndPlant();
}
public interface FourLeggedMammal {
    void moveWithFourLegs();
}
public interface TwoLeggedMammal {
    void moveWithTwoLegs();
}
public interface Hunter {
    void huntForFood();
}
public class Kangaroo implements Animal, Herbivore, TwoLeggedMammal {
    @Override
    public void liveInJungle() {
        System.out.println("I live in Outback country");
    }

    @Override
    public void move() {
        moveWithTwoLegs();
    }

    @Override
    public void moveWithTwoLegs() {
        System.out.println("I like to jump");
    }

    @Override
    public void eat() {
        eatPlant();
    }

    @Override
    public void eatPlant() {
        System.out.println("I like this grass");
    }

    @Override
    public boolean possessIntelligence() {
        return false;
    }
}

public class Lion implements Animal, FourLeggedMammal, Hunter, Carnivore {
    @Override
    public void liveInJungle() {
        System.out.println("I am king of the jungle!");

    }

    @Override
    public void move() {
        moveWithFourLegs();
    }

    @Override
    public void moveWithFourLegs() {
        System.out.println("I like to run sometimes.");
    }

    @Override
    public void eat() {
        eatMeat();
    }

    @Override
    public void eatMeat() {
        System.out.println("I like deer meat");
    }

    @Override
    public boolean possessIntelligence() {
        return false;
    }

    @Override
    public void huntForFood() {
        System.out.println("My females hunt often");
    }
}
public class Teacher implements Human {
    @Override
    public void liveInCivilization() {
        System.out.println("I live in an apartment");
    }

    @Override
    public void moveWithTwoLegs() {
        System.out.println("I wear shoes and walk with two legs one in front of the other");
    }

    @Override
    public void move() {
        moveWithTwoLegs();
    }

    @Override
    public boolean possessIntelligence() {
        return true;
    }

    @Override
    public void huntForFood() {
        System.out.println("My ancestors used to but now I mostly rely on cattle");
    }

    @Override
    public void eat() {
        eatBothMeatAndPlant();
    }

    @Override
    public void eatBothMeatAndPlant() {
        eatPlant();
        eatMeat();
    }

    @Override
    public void eatMeat() {
        System.out.println("I like this bacon");
    }

    @Override
    public void eatPlant() {
        System.out.println("I like this broccoli");
    }
}
Community
  • 1
  • 1
nabster
  • 1,561
  • 2
  • 20
  • 32
4

Of course... Almost all classes implements several interfaces. On any page of java documentation on Oracle you have a subsection named "All implemented interfaces".

Here an example of the Date class.

carexcer
  • 1,407
  • 2
  • 15
  • 27
4

It is true that a java class can implement multiple interfaces at the same time, but there is a catch here. If in a class, you are trying to implement two java interfaces, which contains methods with same signature but diffrent return type, in that case you will get compilation error.

interface One
{
    int m1();
}
interface Two
{
    float m1();
}
public class MyClass implements One, Two{
    int m1() {}
    float m1() {}
    public static void main(String... args) {

    }
}

output :

prog.java:14: error: method m1() is already defined in class MyClass
    public float m1() {}
                 ^
prog.java:11: error: MyClass is not abstract and does not override abstract method m1() in Two
public class MyClass implements One, Two{
       ^
prog.java:13: error: m1() in MyClass cannot implement m1() in Two
    public int m1() {}
               ^
  return type int is not compatible with float
3 errors
NPE
  • 432
  • 5
  • 13
2

Yes, it is possible. This is the catch: java does not support multiple inheritance, i.e. class cannot extend more than one class. However class can implement multiple interfaces.

AlexR
  • 114,158
  • 16
  • 130
  • 208
2

An interface can extend other interfaces. Also an interface cannot implement any other interface. When it comes to a class, it can extend one other class and implement any number of interfaces.

class A extends B implements C,D{...}
Nirmal Dalmia
  • 317
  • 3
  • 12
  • 1
    Actually, an interface can extend any number of interfaces: https://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html – meriam Mar 20 '19 at 11:13