How can we prove that java doesn't support multiple inheritance?
Is there any alternative method to implement Multiple Inheritance in Java?
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.
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
Stringclass toIntegersuperclasses, 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.
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
Java does not support multiple inheritance but in Java you can achieve multiple inheritance using less complex feature called - Interfaces.
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();
}
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.
public class Child extends Parent1, Parent2 {...