Java Reference
In-Depth Information
Drawing a rectangle...
Drawing a circle...
Name: Rectangle
Area: 8.0
Perimeter: 12.0
Name: Circle
Area: 78.53981633974483
Perimeter: 31.41592653589793
You have finished discussing the main rules of declaring classes and methods
abstract
. However, there are many
other rules that govern the use of
abstract
classes and methods in a Java program. Most of those rules (if not all) are
listed below. All rules point to only one basic rule: “Abstract class should be subclassed to be useful and the subclass
should override and provide implementation for the
abstract
methods.”
abstract
even if it does not have an
abstract
method.
•
A class may be declared
abstract
if it declares or inherits an
abstract
method. If the class
overrides and provides implementations for all inherited
abstract
methods and does not
declare any
abstract
methods, it does not have to be declared
abstract
. Although it could be
declared
abstract
.
•
A class must be declared
abstract
class. However, you can declare a variable of the
abstract
class type and call methods using it.
•
You cannot create an object of an
abstract
class cannot be declared
final
. Recall that a
final
class cannot be subclassed,
which conflicts with the requirement of an
abstract
class that it must be subclassed to be
useful in a true sense.
•
An
abstract
class should not declare all constructors
private
. Otherwise, the
abstract
class
cannot be subclassed. Note that constructors of all ancestor classes (including an
abstract
class) are always invoked when an object of a class is created. When you create an object of a
Rectangle
class, constructors for the
Object
class and the
Shape
class are also invoked. If you
declare all constructors of an
abstract
class
private
, you cannot create a subclass for your
abstract
class, which makes it the same as declaring an
abstract final
class.
•
An
abstract
method cannot be declared
static
. Note that an
abstract
method must be
overridden and implemented by a subclass. A
static
method cannot be overridden. However,
it can be hidden.
•
An
abstract
method cannot be declared
private
. Recall that a
private
method is not
inherited and hence it cannot be overridden. The requirement for an
abstract
method is that
a subclass must be able to override and provide implementation for it.
An
abstract
method cannot be declared
native
,
strictfp
, or
synchronized
. These keywords refer to
implementation details of a method. The
native
keyword denotes that a method is implemented in native code
as opposed to Java code. The
strictfp
keyword denotes that the code inside a method uses FP-strict rules for
floating-point computations. Please refer to
http://en.wikipedia.org/wiki/Strictfp
for more details on the
strictfp
keyword and its usage. The
synchronized
keyword denotes that the object on which the method is invoked
must be locked by the thread before it can execute method's code. Since an
abstract
method does not have an
implementation, the keywords that imply an implementation cannot be used for an
abstract
method.
•
An