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:

  1. 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.

  1. 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?

Recommended Answers

All 2 Replies

Yeah, well after spending 20 years doing advanced C++ software design and development, the lack of multiple inheritance in Java drives me nuts! Interfaces are OK, but not the panacea that a lot of java developers think it is... sigh!

I just finished a significant SNMP framework for Java, and the lack of multiple inheritance was a major PITA, and made the resulting work much more complex and difficult than it should have been.

discussing this is just as usefull as discussing why frogs need wings. fact is, Java doesn't support multiple inheritance.
Tandex, when giving reasons why people claim multiple inheritance isn't included, you forgot the two most common ones:

  1. Deadly Diamond of Death - issue
    issume multiple inheritance is possible:

    public class ClassA{
    public void printLine(String toPrint){
    System.out.println("ClassA: " + toPrint);
    }
    }

    public class ClassB{
      public void printLine(String toPrint){
        System.out.printLine(String toPrint){
          System.out.println("ClassB: " + toPrint);
        }
    }
    
    
    
    
    public class ClassC extends ClassA, ClassB{
      public static void main(String[] args){
        ClassC newInstance = new ClassC();
        newInstance.printLine("from ClassC");
      }
    }
    

so, when running ClassC ... how would the JVM be able to tell which printLine method you are trying to call?

and, last but not least, the most important reason:

the creators of Java decided not to implement multiple inheritance.

personally, I haven't encountered anything (yet) that would require multiple inheritance, so stating that Java will HAVE to ... I sincerely doubt it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.