Lecture 12
Abstract Classes
An abstract class is a class whose sole purpose purpose is to be extended.
public abstract class List {
protected int size;
public int length() {
return size;
}
public abstract void insertFront(Object item);
}
Abstract classes don't allow you to create objects directly, although you can declare.
List myList; // Right on.
myList = new List(); // COMPILE-TIME ERROR.
Abstract classes can be extended in the same way as ordinary classes.
An abstract method lacks implementation. One purpose of an abstract method is to guarantee that every non-abstract subclass will implement the method (i.e. every non-abstract subclass of List
must have an implementation for the insertFront
method).
Only abstract class can have abstract method.
What are abstract classes good for? It's all about the interface.
----------------------------------------------------
| An abstract class lets you define an interface |
| - for multiple classes to share, |
| - without defining any of them yet. |
----------------------------------------------------
A big advantage of OO language is that ADT hides implementation.
----------------- The list sorter is built on the foundation of a list
| Application | ADT, and the application is built on the foundation of
----------------- the list sorter. However, it's the application, and
| not the list sorter, that gets to choose what kind of
| calls list is actually used, and thereby obtains special
v features like transaction logging. This is a big
----------------- advantage of object-oriented languages like Java.
| List Sorter |
-----------------
|
| calls
v
-----------------
| List ADT |
-----------------
Java Interfaces
A Java interface is just like an abstract class, except for two differences.
- In Java, a class can inherit from only one class, even if the superclass is an abstract class. However, a class can "implement" (inherit from) as many Java interfaces as you like.
- A Java interface cannot implement any methods, or can it include any fields except "final static" constants. It only contains method prototypes and constants.
public interface Nukeable { // In Nukeable.java
public void nuke();
}
public interface Comparable { // In java.lang
public int compareTo(Object o);
}
public class SList extends List implements Nukeable, Comparable {
[Previous stuff here.]
public void nuke() {
head = null;
size = 0;
}
public int compareTo(Object o) {
[Returns a number < 0 if this < o,
0 if this.equals(o),
> 0 if this > o.]
}
}
The distinction between abstract classes and Java interfaces exists because of technical reasons that you might begin to understand if you take CS 164 (Compilers). Some languages, like C++, allow "multiple inheritance," so that a subclass can inherit from several superclasses. Java does not allow multiple inheritance in its full generality, but it offers a sort of crippled form of multiple inheritance: a class can "implement" multiple Java interfaces.
Why does Java have this limitation? Multiple inheritance introduces a lot of problems in both the definition of a language and the efficient implementation of a language. For example, what should we do if a class inherits from two different superclasses two different methods or fields with the same name? Multiple inheritance is responsible for some of the scariest tricks and traps of the C++ language, subtleties that cause much wailing and gnashing of teeth. Java interfaces don't have these problems.
Because an SList is a Nukeable and a Comparable, we can assign it to variables of these types.
Nukeable n = new SList();
Comparable c = (Comparable) n;
Interfaces can be extended with subinterfaces. A subinterface can have multiple superinterfaces, so we can group several interfaces into one.
public interface NukeAndCompare extends Nukeable, Comparable { }
We could also add more method prototypes and constants, but in this example I don't.
Comparable
Comparable
is a standard interface in the Java library. A class that implement Comparable
has access to Java's sorting library.
For instance, the Arrays class in java.util includes a method that sorts arrays of Comparable objects.
public static void sort(Object[] a) // In java.util
The parameter's type is Object[], but a run-time error will occur if any item stored in a is not a Comparable.