Java Reference
In-Depth Information
If you look at the definition of the
Shape
class, it looks the same as any other concrete classes, except the use
of the
abstract
keyword in the declaration. A class has instance variables and instance methods to define the state
and behavior of its objects. By declaring a class
abstract
, you indicate that the class has some incomplete method
definitions (behaviors) for its objects and it must be considered incomplete for object creation purposes.
What is an incomplete method in a class? A method which has a declaration, but no body, is an incomplete
method. Missing body of a method does not mean an empty body. It means no body. The braces that follow the
method's declaration indicate the body of the method. In the case of an incomplete method, the braces are replaced
with a semicolon. If a method is incomplete, you must indicate it by using the
abstract
keyword in the method's
declaration. Your
Shape
class does not know how to draw a shape until you mention a specific shape. However, one
thing is sure that you should be able to draw a shape no matter what kind of shape it is. In this case, you know the
behavior name (draw), but you do not know how to implement it. Therefore, draw is a good candidate to be declared
as an
abstract
method (or incomplete method) in the
Shape
class. The
Shape
class looks as follows with an
abstract
draw()
method:
public abstract class Shape {
public Shape() {
}
public abstract void draw();
}
When you declare a class
abstract
, it does not necessarily mean that it has at least one abstract method. An
abstract
class may have all concrete methods. It may have all
abstract
methods. It may have some concrete and some
abstract methods. If you have an
abstract
class, it means that an object of that class cannot exist. However, if a class
has an
abstract
method (either declared or inherited), it must be declared
abstract
. Declaring a class as
abstract
is like placing an “under construction” sign in front of a building. If an “under construction” sign is placed in front of
a building, it is not supposed to be used (not supposed to be created in case of a class). It does not matter whether the
building is complete or not. An “under construction” sign is enough to indicate that it cannot be used. However, if some
parts of the building are incomplete (like a class having abstract methods), you must place an “under construction” sign
in front of it (must declare the class
abstract
) to avoid any mishap, in case someone attempts to use it.
■
You cannot create objects of an
abstract
class. If a class has an
abstract
method, declared or inherited, the class
must be declared
abstract
. If a class does not have any
abstract
methods, you can still declare the class
abstract
.
an
abstract
method is declared the same way as any other methods, except that its body is indicated by a semicolon.
Tip
Listing 16-36 has the complete code for your
Shape
class.
Listing 16-36.
An Abstract Shape Class with One Instance Variable, Two Constructors, and One Abstract Method
// Shape.java
package com.jdojo.inheritance;
public abstract class Shape {
private String name;
public Shape() {
this.name = "Unknown shape";
}