Java Reference
In-Depth Information
Extending Interfaces
An interface can extend another interface. In fact, an interface can extend multiple
interfaces. (Don't accuse Java of not allowing multiple inheritance!) Use the
extends
keyword to declare that an interface extends another interface. For example, the following
interface extends the
Drawable
interface:
1. public interface Paintable extends Drawable {
2. public void paint();
3. }
A class that implements
Paintable
must override
paint
and also the three methods in
Drawable
.
Let's look at an example of multiple interface inheritance. The following
Image
interface
extends both
java.lang.Runnable
and
Drawable
:
1. public interface Image extends Runnable, Drawable {
2. public String getFormat();
3. }
A class that implements
Image
must implement the
getFormat
method, as well as the
run
method from
Runnable
and the three methods from
Drawable
.
Multiple Inheritance with Interfaces
While valid, writing an interface that extends multiple interfaces is not a common
occurrence in Java. There are situations where the multiple inheritance makes sense, but
this is not something you will do every day.
Declaring Enumerations
Java 5.0 introduced the concept of enumerations to the Java language, along with a new
keyword:
enum
. An
enumeration
is a fi xed set of constants. An
enum
is a Java class that
represents an enumeration. You use enumerations whenever you have a set of items whose
values are known at compile time. Common uses of enumerations include days of the
week, months of the year, the planets in the solar system, the directions on a compass, or
your favorite fl avors of ice cream. The possibilities for enums are endless, and you should
use them in your Java applications whenever applicable because they provide a type-safe
representation of constant data in your application.
Search WWH ::
Custom Search