Java Magazine, Jan/Feb 2018
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2018 96 new to java 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
You must have JavaScript enabled to view digital editions.