some people say that multiple inheritance in java is not needed because we have interfaces or that using singular inheritance can bring the same results as using a multiple inheritance. But that is not the case. Lets take a look at the example I call “The Workbench Example”
say we have a set of users on a server called workbench. Each user has a specific policy according to the group they reside in. the table below shows each user with their group and policy:
USER (last, first)
Group
Policy #
Dillon, Johnathan
Basic
1
Elise, Barbara
Advanced, Machine
4
Wallace, Mark
Installer, machine
3
Wcjzoidiy, Romaski
Technical, Advanced, machine
3
Darhu, Markham
Owner
2
Ackerman, Peter
Technical, Basic
6
Troitsovskiy, Aleksandre
Administrator, Owner
6
Lynn-Martin, Samantha
HR, Advanced, Machine, Installer
5
Spakzy, Allen
Basic, Installer
1
Plythe-lynn, Brandon
HR, Basic
2
Lexy, Timothy
Installer, Technical
3
Dehls, Arthur
Technical
3
Peaton, Borris
Technical
3
Waharu, Usagi
Administrator
5
Create a program that sorts he users into their groups. Each user must be able to access all of the functions in the specified group classes (get_class_name, get_id_num, authenticate) and contain a copy of the policy specified. Each policy must contain:
when they can log on,
users applied to this,
apps they can use,
maximum data space avail.
*These can be set at random
Now, in c++, this is easy:
//header 1:
class basic
{
//…. code here
};
//header 2
class user
{
//… code here
};
//attempt to create Samantha Lynn-Martin's user class
// assume the rest of the classes are created
//header 3.
class sam : public human_r, advanced, machine, installer, user
{
…. code here
};
in C++, one can inherit multiple classes, which in this case is needed to accommodate the problem stated. This problem, however is much more complex in java. Java can only inherit one class, but can inherit multiple interfaces. Still, it is not possible to work this problem in java. Now lets apply what people say to why multiple inheritance is not needed in java:
- One can just use Singular Inheritance to give the same functionality.
A: No, in most occasions. Take for instance Timothy Lexy, who is both an installation user and a technical user. The installation class can not subclass technical user because that would give base technical users functions that only installation users can have and vica-versa.
- One can use interfaces as a substitute to classes when attempting to multiple inheritance.
A. Not exactly. JavaDocs define an Interface as a class where by default, all members are abstract. Therefore each time the interface is called by a class, one has to write the entire function out for all functions, which not only makes the executable bigger and bloated, but gets tedious over time (C++ classes do not have to be purely funcptr's or virtuals). Luckily, this problem has the ref classes containing only 3 functions, so using an interface would not really be a problem, but what happens if the interface has 10 or 20 or even more functions? If this was in the workplace where you had to solve this problem, the user who would be coding in java would be wasting time rewriting the functions for each classes thatr equires the interface.
IMHO, java will at some point have to implement some form of way to extend more than one class (IE. Changing the amount of classes that can be extended, or a better idea: creating a function or operand that can get the functions and variables of a class and allowing the class to run and even override or write the functions obtained from the ref class). Even other OOP languages, like C# or VB .NET can do multiple inheritance with classes, so why not java?