Java Reference
In-Depth Information
pop-up menu. This serves our intention discussed above: we stated that we did not want in-
stances of class Animal created directly—this class serves only as a superclass. Declaring
the class abstract enforces this restriction.
Only abstract classes can have abstract methods. This ensures that all methods in concrete
classes can always be executed. If we allowed an abstract method in a concrete class, we
would be able to create an instance of a class that lacked an implementation for a method.
Abstract classes with abstract methods force subclasses to override and implement those
methods declared abstract. If a subclass does not provide an implementation for an inherited
abstract method, it is itself abstract, and no instances may be created. For a subclass to be
concrete, it must provide implementations for all inherited abstract methods.
Abstract sub-
class. For a
subclass of an
abstract class to
become concrete,
it must provide
for all inherited
abstract methods.
Otherwise, the sub-
class will itself be
Now we can start to see the purpose of abstract methods. Although they do not provide an
implementation, they nonetheless ensure that all concrete subclasses have an implementation of
this method. In other words, even though class Animal does not implement the act method, it
ensures that all existing animals have an implemented act method. This is done by ensuring that
no instance of class Animal can be created directly, and
all concrete subclasses must implement the act method.
Although we cannot create an instance of an abstract class directly, we can otherwise use an abstract
class as a type in the usual ways. For instance, the normal rules of polymorphism allow us to handle
foxes and rabbits as instances of the Animal class. So those parts of the simulation that do not need
to know whether they are dealing with a specific subclass can use the superclass type instead.
Exercise 10.33 Although the body of the loop in Code 10.6 no longer deals with the Fox
and Rabbit types, it still deals with the Animal type. Why is it not possible for it to treat each
object in the collection simply using the Object type?
Exercise 10.34 Is it necessary for a class with one or more abstract methods to be defined
as abstract? If you are not sure, experiment with the source of the Animal class in the foxes-
and-rabbits-v2 project.
Exercise 10.35 Is it possible for a class that has no abstract methods to be defined as
abstract? If you are not sure, change act to be a concrete method in the Animal class by
giving it a method body with no statements.
Exercise 10.36 Could it ever make sense to define a class as abstract if it has no abstract
methods? Discuss this.
Exercise 10.37 Which classes in the java.util package are abstract? Some of them
have Abstract in the class name, but is there any other way to tell from the documentation?
Which concrete classes extend them?
Exercise 10.38 Can you tell from the API documentation for an abstract class which (if any)
of its methods are abstract? Do you need to know which methods are abstract?
Search WWH ::

Custom Search