Java Magazine, Jan/Feb 2018
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2018 95 new to java Note that because Car inherits only the type including the signature of the act method but no code it must itself supply the code to implement the type the implementation of the act method before you can create objects from it So far this is just single inheritance and could have been achieved by inheriting a class But imagine now that there is also a list of all objects to be drawn on screen which is not the same as the list of actors because some actors are not drawn and some drawn objects are not actors private List Drawable drawables You might also want to save a simulation to permanent storage at some point and the objects to be saved might again be a different list To be saved they need to be of type Serializable private List Serializable objectsToSave In this case if the Car objects are part of all three lists they act they are drawn and they should be saved the class Car 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
You must have JavaScript enabled to view digital editions.