Java Magazine, September/October 2016
Faculty Student PhDStudent Person Faculty Student PhDStudent ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 48 new to java The decision to link type inheritance and code inheritance in Java is a language design choice it was done because it is often useful but it is not the only way a language can be designed Other programming languages allow inheriting code without inheriting the type such as C private inheritance or inheriting type without code which Java also supports as I explain shortly Multiple Inheritance The next idea entering the mix is multiple inheritance a class may have more than one superclass Let me give you an example PhD students at my university also work as instructors In that sense they are like faculty they are instructors for a class have a room number a payroll number and so on But they are also students they are enrolled in a course have a student ID number and so on I can model this as multiple inheritance see Figure 1 PhDStudent is a subclass of both Faculty and Student This way a PhD student will have the attributes of both students and faculty Conceptually this is straightforward In practice however the language becomes more complicated if it allows multiple inheritance because that introduces new problems What if both superclasses have fields with the same name What if they have methods with the same signature but diferent implementations For these cases I need language constructs that specify some solution to the problem of ambiguity and name overloading However it gets worse Diamond Inheritance A more complicated scenario is known as diamond inheritance see Figure 2 This is where a class PhDStudent has two superclasses Faculty and Student which in turn have a common superclass Person The inheritance graph forms a diamond shape Now consider this question if there is a field in the toplevel superclass Person in this case should the class at the bottom PhDStudent have one copy of this field or two It inherits this field twice after all once via each of its inheritance branches The answer is it depends If the field in question is say an ID number maybe a PhD student should have two a student ID and a faculty payroll ID that might be a diferent number If the field is however the persons family name then you want only one the PhD student has only one family name even though it is inherited from both superclasses In short things can become very messy Languages that allow full multiple inheritance need to have rules and constructs to deal with all these situations and these rules are complicated Type Inheritance to the Rescue When you think about these problems carefully you realize that all the problems with multiple inheritance are related to inheriting code method implementations and fields Multiple code inheritance is messy but multiple type inheritance causes no problems This fact is coupled with another observation multiple code inheritance is not terribly important because you can use delegation using a reference to another object instead but multiple subtyping is often very useful and not easily replaced in an elegant way Figure 1 An example of multiple inheritance Figure 2 An example of diamond inheritance
You must have JavaScript enabled to view digital editions.