Java Multiple Inheritance - using interface
In multiple inheritance there are multiple base classes accessed by a single derived class.
Java doens't support the concept of multiple inheritance by extending multiple classes. It is required in most of the real time applications hence it is possible by using interfaces.
Problems while extending multiple classes
- In ealier programming languages like C++ there was a problem of ambiguity due to the multiple paths. Java totally removed this ambiguity problem in the Multiple inheritance
- Ex- Consider there are 2 base classes having same method called display(). The third while calling the display method will confuse to call either from 1st base class or from 2nd base class.
- Thats why java prevents to extend multiple classes at time. In some user applications we need to use the multiple inheritance concept. Java allows us to do by using interfaces by implementing them into a class.
Using interfaces for Multiple Inheritance
Using interfaces we can implement more than one interface into the class.
Syntax
interface Base1
{
// method declarations;
// final field = value;
}
interface Base2
{
// method declarations;
// final field = value;
}
interface Base3
{
// method declarations;
// final field = value;
}
class DerivedClass implements Base1,Base2,Base3
{
// interface method implementations;
// body of Derived class
}
Example