abstract
—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:
abstract void moveTo(double deltaX, double deltaY);
abstract
, as in:
public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); }
abstract
.
Interfaces
section) are implicitly abstract, so the abstract
modifier is not used with
interface methods (it could be—it's just not necessary).
static
and
final
, 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
or Cloneable
, 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).
GraphicObject
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 the
moveTo
method. GraphicObject
also declares abstract methods for methods, such as
draw
or resize
, that need to be implemented by all
subclasses but must be implemented in different
ways. The GraphicObject
class can look
something like this:
abstract class GraphicObject { int x, y; ... void moveTo(int newX, int newY) { ... } abstract void draw(); abstract void resize(); }
GraphicObject
, such
as Circle
and Rectangle
, must provide
implementations for the draw
and resize
methods:
class Circle extends GraphicObject { void draw() { ... } void resize() { ... } } class Rectangle extends GraphicObject { void draw() { ... } void resize() { ... } }
Interfaces
, 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 be
abstract
. For example,
abstract class X implements Y { // implements all but one method of Y } class XX extends X { // implements the remaining method in Y }
X
must be abstract
because it does not fully
implement Y
, but class XX
does, in fact, implement Y
.
static
fields and static
methods.
You can use these static members with a class reference—for example, AbstractClass.staticMethod()
—as
you would with any other class.