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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.