1

How can we prove that java doesn't support multiple inheritance?

Is there any alternative method to implement Multiple Inheritance in Java?

6
  • 10
    Prove? There is a language specification. Multiple inheritance is not in there. Commented Apr 16, 2014 at 6:14
  • 3
    Try compiling public class Child extends Parent1, Parent2 {... Commented Apr 16, 2014 at 6:15
  • 2
    No proof is required of statements in the JLS. You just cite them. Commented Apr 16, 2014 at 6:17
  • 4
    JLS 8.1.4 describes the structure of super class declaration. Note, that a class can implement several interfaces. For that, see JLS 8.1.5. (I looked it up in the JLS of Java 8.) Commented Apr 16, 2014 at 6:18
  • 1
    You should also note that proof by compilation isn't valid, as it doesn't exclude the possibility of compiler bugs, however remote. Commented Apr 16, 2014 at 6:24

6 Answers 6

6

How can we prove that java doesnt support multiple inheritance?

Create 3 classes in the same package, let's say Test1, Test2 and Test3. And try this. You will get an error:

public class Test extends Test2, Test3
{
    public static void main(String[] a)
    {
        // ...
    }
}

Is there any alternative method to implement Multiple Inheritance in Java

Yes, you can make your class implement multiple (1 or more) interfaces.

Sign up to request clarification or add additional context in comments.

Comments

4

There is no multiple inheritance in java as it is specified by the language specification.

To prove it simply try to extend 2 unrelated classes:

public class MyClass extends Number, AbstractList {

}

This will result in a compile error.

You can do one thing however:

Excerpt from java magic part 4: Unsafe

Multiple Inheritance

There is no multiple inheritance in java. Correct, except we can cast every type to every another one, if we want.

long intClassAddress = normalize(getUnsafe().getInt(new Integer(0), 4L));
long strClassAddress = normalize(getUnsafe().getInt("", 4L));
getUnsafe().putAddress(intClassAddress + 36, strClassAddress);

This snippet adds String class to Integer superclasses, so we can cast without runtime exception.

(String) (Object) (new Integer(666))

I really do not recommend this. It is considered a "best practice" to implement multiple interfaces if you want something which resembles multiple inheritance or as others pointed out you can use composition and delegation as well.

1 Comment

Perhaps an example other than String would be better, because String is final, and so, even if this were possible, it would still fail to compile.
3

Is there any alternative method to implement Multiple Inheritance in Java

Depending on what you need, you can get almost the same effect with

  • implementing multiple interfaces

  • using object composition and delegating method calls to the objects that implement the pieces you would otherwise have inherited

Comments

2

Java does not support multiple inheritance but in Java you can achieve multiple inheritance using less complex feature called - Interfaces.

1 Comment

No, you don't. Interface provides only a subset of true multiple inheritance properties. It's wrong and misleading to consider these two terms as interchangeable.
1

If an one class directly inherited from two or more classes it will be multiple inheritances. But in java this is not allowed.

We don’t need a proof as it is specified in java language specification.

However If you need a proof try to compile below(This will not compile)

class A{
 void doSomething(){
   System.out.println(“from class A”);
 }
}


class B{
 void doSomething(){
  System.out.println(“from class B”);
 }
}


class C extends A,B{
  public static void main(String arr[]){
   C cob= new C();
   cob.doSomething();//now how does compiler knows which super method to call?
  }
}

The reason that Java's creators chose not to allow multiple inheritance is that it can become quite messy. But indeed you can have something like this.

public interface InterfaceA {

  public void doSomething();
}



public interface InterfaceB {

 public void doSomething();
}



 public interface InterfaceC extends InterfaceA, InterfaceB {

  //same method is declared in InterfaceA and InterfaceB both
  public void doSomething();

 }

Comments

0
class A
{
  public void methodAClassA()
  {
  System.out.println("In ClassA MethodA");
  }
}
class B extends A{
  public void methodAClassA(){
  System.out.print("in Class B Method ClassA");
  }
}
class C extends A,B {}

Which is not possible because if you use the above code the compiler does not know which instance method could be used in the class C So they made it as error it will show the compilation error.

3 Comments

indentation hurts less.
The reason advanced, mis-spelt, and hidden inside the code, is not correct. Other languages manage it somehow. -1
You fixed the form, but not the content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.