Java Magazine, September/October 2016
The Evolving Nature of Interfaces Understanding multiple inheritance in Java MICHAEL KÖLLING ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 47 new to java In the New to Java series I try to provide benefit by picking topics that invite a deeper understanding of the conceptual background of a language construct Often novice programmers have a working knowledge of a concept that is they can use it in many situations but they lack a deeper understanding of the underlying principles that would lead to writing better code creating better structures and making better decisions about when to use a given construct Java interfaces are just such a topic In this article I assume that you have a basic understanding of inheritance Java interfaces are closely related to inheritance as are the extends and implements keywords So I will discuss why Java has two diferent inheritance mechanisms indicated by these keywords how abstract classes fit in and what various tasks interfaces can be used for As is so often the case the story of these features starts with some quite simple and elegant ideas that lead to the definition of concepts in early Java versions and the story gets more complicated as Java advances to tackle more intricate real world problems This leads to the introduction of default methods in Java 8 which muddy the waters a bit A Little Background on Inheritance Inheritance is quite straightforward to understand in principle a class can be specified as an extension of another class In such a case the present class is called a subclass and the class its extending is called the superclass Objects of the subclass have all the properties of both the superclass and the subclass They have all fields defined in either subclass or superclass and also all methods from both So far so good Inheritance is however the equivalent of the Swiss Army knife in programming it can be used to achieve some very diverse goals I can use inheritance to reuse some code I have written before I can use it for subtyping and dynamic dispatch I can use it to separate specification from implementation I can use it to specify a contract between diferent parts of a system and I can use it for a variety of other tasks These are all important but very diferent ideas It is necessary to understand these diferences to get a good feel for inheritance and interfaces Type Inheritance Versus Code Inheritance Two main capabilities that inheritance provides are the ability to inherit code and the ability to inherit a type It is useful to separate these two ideas conceptually especially because standard Java inheritance mixes them together In Java every class I define also defines a type as soon as I have a class I can create variables of that type for example When I create a subclass using the extends keyword the subclass inherits both the code and the type of the superclass Inherited methods are available to be called Ill refer to this as the code and objects of the subclass can be used in places where objects of the superclass are expected thus the subclass creates a subtype Lets look at an example If Student is a subclass of Person then objects of class Student have the type Student but they also have the type Person A student is a person Both the code and the type are inherited PHOTOGRAPH BY JOHN BLYTHE
You must have JavaScript enabled to view digital editions.