Software Development

Abstract classes and interfaces

This week’s post briefly describes the different roles played by abstract classes and interfaces in the Java programming environment.  An illustration is given for the use of an interface, along with a justification as to why the interface approach is more appropriate than using an abstract class.

An abstract class is a programming class with many of the same properties as any other class (Murach, 2011, pp. 266-267).  An abstract class has many of the same characteristics of any other Java class; fields, constructors, and methods.  Any abstract methods within the abstract superclass must be defined in the subclasses.  As a model only, the abstract class cannot be instantiated, i.e. objects cannot be directly created from the abstract superclass. The abstract class is most useful when a superclass is desired to serve as a generic type to be inherited by two or more subclasses (Lowe, 2017, p. 301). 

To illustrate the use of abstract classes, the JavaFX Application class provides a meaningful example (JavaFX Class Application Javadoc, 2019).  JavaFX, currently the standard graphical user interface (GUI) used in Java, offers the programmer the ability to open a window on the desktop and to run an application which interacts with users through the graphical window.  Users can enter text, click on buttons, checks boxes, and more as part of the program.  But if there were no window, there would be no GUI application.  The JavaFX code ensures the creation of a window by using an abstract class.

The JavaFX abstract class Application provides the basic framework for all JavaFX applications.  Most importantly, the Application class contains the abstract method start(Stage primaryStage).  All JavaFX applications inherit this important method, which is used to launch the GUI application.  The Stage class is a descendant of Window and is used then to control the appearance of a graphical user interface.  Without a stage (window) properly defined there can be no GUI.  In this case, the abstract class ensures the essential components are present for the GUI to operate.

Interfaces are also used in JavaFX GUI components.  Elements that appear within the GUI window (primaryStage) often have different attributes, many of which area constructed through the use of an interface.  Consider, for example, the Button class in JavaFX (JavaFX Class Button Javadoc, 2019).  The Button class implements three interfaces; Styleable, EventTarget, and Skinnable.  Each of these three classes offers the programmer different attributes and methods to be used with a JavaFX button object.  For example, a button class without Styleable would offer a very narrow range of appearance for the button itself.  The Styleable interface allows the programmer to apply CSS meta data to the button object. 

References                                          

Deitel, P., & Deitel, H. (2012). Java: How to Program 9th Edition. Boston: Prentice Hall.

Lowe, D. (2017). Java All-In-One for dummies, 5th edition. Hoboken: John Wiley & Sons.

Murach, J. (2011). Java Programming, 4th edition. Fresno: Mike Murach & Associates.

Oracle. (2019, March 19). JavaFX Class Application Javadoc. Retrieved from docs.oracle.com: https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html

Oracle. (2019, March 19). JavaFX Class Button Javadoc. Retrieved from docs.oracle.com: https://docs.oracle.com/javase/8/javafx/api/toc.htm

StackOverflow.Com. (2012, December 9). Responsibilities and use of service and DAO Layers. Retrieved from StackOverflow.Com: https://stackoverflow.com/questions/13785634/responsibilities-and-use-of-service-and-dao-layers

Leave a Reply