Running on Java 19-ea+23-1706 (Preview)
Home of The JavaSpecialists' Newsletter

1.7 StaticWordLibrary Structure

Juppies 2 by Dr Heinz M. Kabutz

The WordLibrary is an abstract class, meaning it is a bit like a contract of what we are going to offer, but allows us to have different concrete implementations of that contract. The first such concrete implementation class is the StaticWordLibrary. Note that the WordLibrary is abstract, with three of its methods abstract too. All of these methods have to be defined again in the subclass StaticWordLibrary, this time with a proper body that does something useful.

Note that I made the StaticWordLibrary final, which means that it cannot be subclassed further. This makes my design more restrictive, thus preventing someone from further extending my class (with the extends keyword). When designing new classes, I recommend making all the classes final, otherwise you will be responsible for maintaining it if someone makes a subclass of it. When you declare it as "final", they will have to beg you to allow them to subclass, and you can ask them for clarification why they want to subclass this particular class.

Most courses that introduce Java speak about subclassing as if it were the bee's knees. In reality, we have to be careful, because we can easily paint ourselves into a corner. A better approach is to use polymorphism to declare a contract (like our WordLibrary), with different implementations (poly - many, morphi - form).


When you load these comments, you'll be connected to Disqus. Privacy Statement.

Table of Contents

We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '22

Superpack '22 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...

Java Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.