Java Magazine, September/October 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 50 new to java can be defined to implement all three interfaces class Car implements Actor Drawable Serializable Situations like this are common and allowing multiple supertypes enables you to view a single object the car in this case from diferent perspectives focusing on diferent aspects to group them with other similar objects or to treat them according to a certain subset of their possible behaviors Javas GUI event processing model is built around the same idea event handling is achieved via event listeners interfaces such as ActionListener that often just implement a single method so that objects that implement it can be viewed as being of a listener type when necessary Abstract Classes I should say a few words about abstract classes because it is common to wonder how they relate to interfaces Abstract classes sit halfway between classes and interfaces they define a type and can contain code as classes do but they can also have abstract methods methods that are specified only but not implemented You can think of them as partially implemented classes with some gaps in them code that is missing and needs to be filled in by subclasses In my example above the Actor interface could be an abstract class instead The act method itself might be abstract because it is diferent in each specific actor and there is no reasonable default but maybe it contains some other code that is common to all actors In this case I can write Actor as an abstract class and the inheritance declaration of my Car class would look like this class Car extends Actor implements Drawable Serializable If I want several of my interfaces to contain code turning them all into abstract classes does not work As I stated before Java allows only single inheritance for classes that means only one class can be listed after the extends keyword Multiple inheritance is for interfaces only There is a way out though default methods which were introduced in Java 8 Ill get to them shortly Empty Interfaces Sometimes you come across interfaces that are empty they define only the interface name and no methods Serializable mentioned previously is such an interface Cloneable is another These interfaces are known as marker interfaces They mark certain classes as possessing a specific property and their purpose is more closely related to providing metadata than to implementing a type or defining a contract between parts of a program Java since version 5 has had annotations which are a better way of providing metadata There is little reason today to use marker interfaces in Java If you are tempted look instead at using annotations A New Dawn with Java 8 So far I have purposely ignored some new features that were introduced with Java 8 This is because Java 8 adds functionality that contradicts some of the earlier design decisions of the language such as only single inheritance for code which makes explaining the relationship of some constructs quite dificult Arguing the diference between and justification for the existence of interfaces and abstract classes for instance becomes quite tricky As I will show in a moment interfaces in Java 8 have been extended so that they become more similar to abstract classes but with some subtle diferences In my explanation of the issues I have taken you down the historical path explaining the pre Java 8 situation first
You must have JavaScript enabled to view digital editions.