I learnt that Java did not allow multiple inheritance using abstract classes for simplicity.

In java, after a good design of class hierarchy, SubType would not get into trouble of same method prototypes or same name variable, coming from both interface and abstract class as parent types, Because abstract class and interface are discovered(but not designed) based on implementation classes. I guess this is what it meant.

Here is the problem Java code, that still has multiple inheritance issue, using interface keyword:

abstract class SuperType1 {
    protected int item;

interface SuperType2{
    final float item=2.0;
public class SubType extends SuperType1 implements SuperType2{
    public static void main(String[] args){
        SubType s = new SubType(); 
        //s.item = 2; //Compiler knows that 'item' is only mutable in 'class SuperType1', this code should not fail.


As per above program, What exactly does it mean to say that, Java omits multiple inheritance?

  • 7
    Java omits multiple class inheritance, but allows multiple interface inheritance. This is how Java has always worked. – Robert Harvey Nov 23 '14 at 3:09
  • 1
    @raptortech97 this isn't true. Every such definition in an interface is just implicitly public static final. The value of item cannot be changed and it can be accessed like this: SuperType2.item. I wouldn't overuse it though and the naming conventions are off. Without the ambiguity, s.item would also work but this is not the right way to access this constant and most code analysis tools will consider it a violation. The code does not compile because of the assignment of a double 2.0 to a float (loss of precision) and the ambiguous use of s.item. – toniedzwiedz Nov 23 '14 at 8:14
  • 2
    @raptortech97 Actually the above code will compile using a Java 8 compiler. – Random42 Nov 23 '14 at 13:28

The item field in SuperType2 class is static so there is no inheritance; there is just an ambiguous reference . All fields from interfaces are static and public by default. Java would have been better designed if static fields and methods can only be accessed through class name and not through a reference; but this is not a major flaw.

Since version 8, Java has the diamond problem and multiple inheritance because interfaces can have default methods. In the case of multiple inheritance the code won't compile unless you overwrite the problematic method and specify how it should behave (you can also choose the implementation from a supperclass/interface by using the syntax SuperType.super.method() in the subclass implementation).

  • yes, i get ambiguous error in java 7, all fields in interface are public/static/final. So, in addition to being static, it is not mutable, so java has to know at compile time that s.item is from SuperType1.looks like, java 8 has introduced lot of contradictory concepts like having method implementations in interface{}. Do you recommend learning new java using java 8?java 8 will make java programmer change his design approach in making use of interface and abstract class. tough time fore me as java beginner. – overexchange Nov 24 '14 at 1:09
  • 2
    @overexchange Being final does not mean that the object is not mutable; just that the reference to an object cannot be changed to point to another object, but the referring object can be mutable. I recommend learning Java 8 because introduces neat concepts which boost productivity. More interesting than default methods are lambda expressions and Streams API. – Random42 Nov 24 '14 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.