Abstract Classes and Interfaces

This post briefly describes the different roles played by abstract classesand interfaces in the Java programming environment.  An illustration is given for the use of aninterface, along with a justification as to why the interface approach is moreappropriate than using an abstract class.

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

Toillustrate the use of abstract classes, the JavaFX Application class provides ameaningful example (JavaFX Class Application Javadoc, 2019).  JavaFX, currently the standard graphical userinterface (GUI) used in Java, offers the programmer the ability to open awindow on the desktop and to run an application which interacts with usersthrough the graphical window.  Users canenter text, click on buttons, checks boxes, and more as part of theprogram.  But if there were no window,there would be no GUI application.  TheJavaFX code ensures the creation of a window by using an abstract class.

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

Interfacesare also used in JavaFX GUI components.  Elementsthat appear within the GUI window (primaryStage) often have differentattributes, many of which area constructed through the use of aninterface.  Consider, for example, theButton 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 attributesand methods to be used with a JavaFX button object.  For example, a button class without Styleablewould offer a very narrow range of appearance for the button itself.  The Styleable interface allows the programmerto 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

Oracle. (2019, March 19). JavaFX Class Button Javadoc. Retrieved from  docs.oracle.com

StackOverflow.Com. (2012, December 9). Responsibilities and use of service and DAO Layers. Retrieved from   StackOverflow.Com

Leave a Comment

Your email address will not be published. Required fields are marked *