Saturday, April 8, 2017

Multiple Inheritance

Multiple inheritance is not allowed in Java. In Java one class can only inherit from one superclass not more. It will cause compilation error when one class tries to extends more than one superclass. Another reason it is not allowed is that it will cause ambiguity if subclass tries to override methods from superclass. It doesn't know which methods should be overridden from. It is so called Deadly Diamond of Dead (DDD) issue. By default even Java doesn't allow multiple inheritance but we can achieve it by implementing interfaces. Interface is one of important concept in object-oriented programming.

class A{ }

class B{ }

class C extends A{ }

class C extends B{ }

public class MultipleTest
{
  public static void main(String args[])
  {
  
  }
}

Result:
MultipleTest.java:16: error: duplicate class: C
class C extends B
^
1 error

No comments:

Post a Comment

Abstraction vs Interface

Abstract Interface method abstract and concrete abstract only, support default and static method ...