In: Computer Science
Based on what you know about object oriented programming, inheritance, and polymorphism, why do you think it is not possible to write code like this in Java:
public class X extends Y, Z { // ... }
...but can write code like this:
public class A implements B, C { //... }
Java doesn't support Multiple inheritance, to get around this the concept of Interfaces is used.
Java supports a limited form of multiple inheritance.
Java has a rule that a class can extend only one abstract class, but can implement multiple interfaces (fully abstract classes)
In Java A class can extend at most one abstract class, but may
implement many interfaces.
Abstract class = a class that contains at least
one abstract method, and can also contain concrete (implemented)
methods
Interface = a class that is fully abstract — it
has abstract methods, but no concrete
methods
Explanation:
In java a class can be an abstract class without being a fully abstract class. It can be a partially abstract class.
Imagine that that we have two partially abstract classes A and B. Both have some abstract methods, and both contain a non-abstract method called foo().
Also imagine that Java allows a class to extend more than one abstract class, so we can write a class C that extends both A and B. And imagine that C doesn’t implement foo().
So now there is a problem. Suppose we create an instance of C and invoke its foo() method. Which foo() should Java invoke? A.foo() or B.foo()?
This is a form of limited multiple inheritance. Basically, the rule
says that you can inherit from (extend) as many classes as you
want, but if you do, only one of those classes can
contain concrete (implemented) methods.
Some languages allow multiple inheritance like C++ and python , Java has limitation.