Java Reference
In-Depth Information
Suppose that we create a new methodology for computing the strength of a fictional
character. Then, if the user has saved past results, this value would be meaningless and
can result in the program behaving erroneously. Therefore, we will rewrite our program and
only provide a public interface that allows us to compare two fictional characters.
Remember that we used the
compareTo
method to compare two strings.
⎧
⎨
positive integer
if s1
>
s2
s1
.
compareTo
(
s2
)=
negative integer
if s1
<
s2
(8.1)
⎩
0
if s1=s2
For strings,
<
and
>
refer to the lexicographical ordering of strings. In general, we can
use the
compareTo
method to compare two objects and define the meaning of an object to
be smaller, equal to, or greater than another object.
In Java, there is a class that includes the
compareTo
method. It is a simple class; it
only includes the method and defines it as an abstract method. We can tell the world that
our class supports the
compareTo
method by inheriting from this class. This class is special
because it only contains the abstract
compareTo
method. It does not contain any methods
with bodies, constructors, or variables. In Java, such classes are referred to as
interfaces
.
An
interface
is a special class that contains only methods without bodies (i.e.,
abstract methods). The
abstract
keyword is not used when defining a method inside
an interface because all the methods are abstract. An interface cannot contain regular
variables, but it can contain constants (i.e.,
final static
variables). An interface
cannot contain constructors.
Note that an interface is similar to an abstract class because one cannot directly create an
object that belongs to an interface or an abstract class. However, there is a subtle difference.
Since all the methods in an interface are abstract and there are no instance variables inside,
we will never need to use the
super
keyword to refer to an interface object. In other words,
an object for an interface is never created. Conversely, an object for an abstract class is
created as part of the super object of a subclass. Since we cannot use the
super
keyword
when we inherit from an interface and a super object is not created, Java allows us to inherit
from multiple interfaces.
In Java, inheriting from an interface is different than inheriting from a class. When
inheriting from an interface, a super object is not created. In order to emphasize the
difference, Java uses the following terminology. A class
extends
from another class,
but it
implements
an interface. A class implementing an interface means that the
class overrides and implements (i.e., includes the bodies of) all the methods of the
interface. In Java, one can extend a single class, but implement multiple interfaces.
The interface that contains the
compareTo
method is called
Comparable
. Therefore,
the
FictionalCharacter
class can implement this interface and override the
compareTo
method. Here is the new implementation.
public abstract class
FictionalCharacter
implements
Comparable
<
FictionalCharacter
>{
private
String name;
public
FictionalCharacter ()
{
}