Java Reference
In-Depth Information
As you can see, no method body is present. The
abstract
modifier can be used only on in-
stance methods. It cannot be applied to
static
methods or to constructors.
A class that contains one or more abstract methods must also be declared as abstract by
preceding its
class
declaration with the
abstract
modifier. Since an abstract class does not
define a complete implementation, there can be no objects of an abstract class. Thus, at-
tempting to create an object of an abstract class by using
new
will result in a compile-time
error.
When a subclass inherits an abstract class, it must implement all of the abstract methods
in the superclass. If it doesn't, then the subclass must also be specified as
abstract
. Thus,
the
abstract
attribute is inherited until such time as a complete implementation is achieved.
Using an abstract class, you can improve the
TwoDShape
class. Since there is no mean-
ingful concept of area for an undefined two-dimensional figure, the following version of
the preceding program declares
area( )
as
abstract
inside
TwoDShape
, and
TwoDShape
as
abstract
. This, of course, means that all classes derived from
TwoDShape
must override
area( )
.