What is multiple inheritance? It is the ability for a class to be derived from more then one class. To fully appreciate what Java can do in this regard we must take
A Quick Detour into C++
C++ allows multiple inheritance easily, depending on how the programmer sets up the base classes, etc... Consider the following example in C++:
#include <iostream>
#include <string>
using namespace std;
//base class
class Animal {
protected: //derived classes get these
int age;
string name;
public:
Animal() {};
string getName() {return name;};
int getAge() {return age;};
};
//Bird is a derived class from Animal
class Bird : public virtual Animal {
public:
void birdNoise() { cout << "Bird Noise!\n";};
Bird() {};
};
//Horse also derives from Animal
class Horse : public virtual Animal {
public:
void horseNoise() { cout << "Horse Noise!\n";};
Horse() {};
};
class Pegasus : public Bird, public Horse {
//reserved for future items
public:
Pegasus(string n, int a) {name = n; age = a;};
};
int main()
{
Pegasus* peggi = new Pegasus("Pegasus", 5); //create the pegasus object
cout << peggi->getName() << "'s age is " << peggi->getAge() << endl;
peggi->birdNoise();
peggi->horseNoise();
delete peggi;
return 0;
}
A possible UML diagram based on the following:

We set up all of that infrastructure to allow Pegasus to be both a bird and a horse, which is important because he's a flying horse. While on a small scale this seems unnecessary and overcomplicated, as a project grows in both size and scope, determining how objects relate to each other will become more apparent.
But wait, you say, this is the Java tutorial section...you are absolutely right, let's not digress:
Back to Java
We can't do the above directly with Java (indirectly we can though), as it only supports single direct inheritance, such as:
public class Animal {
//animal stuff
}
public class Bird extends Animal {
//Bird stuff
}
//However we can't have another class extend Bird vis a vis extending Animal as well
What we can do is use an interface to simulate multiple inheritance:
//Base class
public class Animal {
protected int age;
protected String name;
public Animal() {}
public String getName() {return name;}
public int getAge() {return age;}
}
Now, instead of having Bird and Horse classes extend Animal (single inheritance), we'll make them interfaces. Any class that implements an interface is required to override the methods presented in them. This is for a programmer's own protection:
//****************SYNTAX***********************
public interface interfaceName {
void abstractMethod();
int overrideMe();
//etc...
}
//**********************************************
public interface Bird {
//all implementers have to overwrite methods
void birdNoise();
}
public interface Horse {
//all implementers have to overwrite methods
void horseNoise();
}
The java syntax for inheritance and interfaces:
public class className extends superClass implements interfaceOne, interfaceTwo, ...
Here is the Pegasus class (from above, converted to Java) with a main() example:
import java.io.*;
//single inheritance, multiple interfaces
public class Pegasus extends Animal implements Bird, Horse {
public Pegasus(String n, int a)
{
name = n;
age = a;
}
//overriding interface methods
public void horseNoise()
{
System.out.println("Horse Noise!");
}
public void birdNoise()
{
System.out.println("Bird Noise!");
}
public static void main(String[] args)
{
Pegasus peggi = new Pegasus("Pegasus", 5);
System.out.println(peggi.getName() + "'s age is " + peggi.getAge());
peggi.birdNoise();
peggi.horseNoise();
}
}
A possible UML diagram based on the java implementation:

A visual is often the best learning aid. Quick synopsis: Based on interfaces we can "fake" multiple inheritance in java since only direct single inheritance is supported. We therefore extend to the base class we plan on using and then use interfaces to allow the object to have access to as many functions as we want. Remember, we must write each class's own implementation for each method in the interface. In large projects, using interfaces allows us to control the flow of code and data access, which is crucial. Hopefully you found this tutorial helpful. You are free use the code/images in any manner that you see fit. Happy coding!
--KYA






MultiQuote




|