An abstract class is a class that is declaredabstract
—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
If a class includes abstract methods, the class itself must be declaredabstract void moveTo(double deltaX, double deltaY);abstract
, as in:When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declaredpublic abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); }abstract
.
Note: All of the methods in an interface (see theInterfaces
section) are implicitly abstract, so theabstract
modifier is not used with interface methods (it could be—it's just not necessary).Abstract Classes versus Interfaces
Unlike interfaces, abstract classes can contain fields that are notstatic
andfinal
, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of
Comparable
orCloneable
, for example.By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).
An Abstract Class Example
In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects—for example: position, fill color, and moveTo. Others require different implementations—for example, resize or draw. AllGraphicObject
s must know how to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object—for example,GraphicObject
, as shown in the following figure.Classes Rectangle, Line, Bezier, and Circle inherit from GraphicObject
First, you declare an abstract class,
GraphicObject
, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and themoveTo
method.GraphicObject
also declares abstract methods for methods, such asdraw
orresize
, that need to be implemented by all subclasses but must be implemented in different ways. TheGraphicObject
class can look something like this:Each non-abstract subclass ofabstract class GraphicObject { int x, y; ... void moveTo(int newX, int newY) { ... } abstract void draw(); abstract void resize(); }GraphicObject
, such asCircle
andRectangle
, must provide implementations for thedraw
andresize
methods:class Circle extends GraphicObject { void draw() { ... } void resize() { ... } } class Rectangle extends GraphicObject { void draw() { ... } void resize() { ... } }When an Abstract Class Implements an Interface
In the section onInterfaces
, it was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to beabstract
. For example,In this case, classabstract class X implements Y { // implements all but one method of Y } class XX extends X { // implements the remaining method in Y }X
must beabstract
because it does not fully implementY
, but classXX
does, in fact, implementY
.Class Members
An abstract class may havestatic
fields andstatic
methods. You can use these static members with a class reference—for example,AbstractClass.staticMethod()
—as you would with any other class.