7.9. Interfaces and Abstract Classes

Suppose that you want to define a class to act as a parent of other classes, but you don't want to let people directly instantiate the class. For instance, you might want to provide some common behavior in the class, but the class will not have enough information to be used by itself. In this case, Java lets you declare a class abstract, and the compiler will not let you build an instance of the class. For instance, Listing 7.9 shows an abstract Shape class. The example says that all Shape subclasses will have methods to look up and set locations (getX, getY, and so forth) but that you are prohibited from directly building a Shape object.

Listing 7.9. Shape.java
 /** The parent class for all closed, open, ...

Get Core Web Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.