Chapter 14: Abstract Classes and Interfaces

advertisement
Computer Science Notes
Chapter 14
Page 1 of 6
Chapter 14: Abstract Classes and Interfaces
These notes are meant to accompany Introduction to Java Programming: Brief Version, eighth edition by Y.
Daniel Lang.
Programming Skills in a Nutshell:
At the end of this chapter you should have the following programming skills:
1. To recognize when to create an interface that gives common functionality across several classes.
2. To recognize when to create an abstract class.
3. To correctly overload or override methods of a superclass in one of its subclasses.
4. To correctly implement some of the fundamental methods in the Object superclass.
Abstract classes and interfaces are two ways of guaranteeing that classes have the same behavior in
certain ways.
Abstract classes are used when the classes are arranged in a hierarchy.
Example: circles, rectangles, and triangles are all subcases of geometric objects that have areas and perimeters.
 they can all have getArea() and getPerimeter() methods (“common behavior”), even though the
calculations behind those methods are different.
Interfaces are used when the classes are not hierarchical.
Classes for automobiles and humans are not hierarchical, but both objects have mass… both can have a
getMass() method
Computer Science Notes
Chapter 14
Page 2 of 6
Book’s Statement of Skills:
1. To design and use abstract classes. (11.2)
2. To process a calendar using the Calendar and GregorianCalendar classes. (11.3)
3. To specify common behavior for objects using interfaces. (11.4)
4. To define interfaces and declare classes that implement interfaces. (11.4)
5. To define a natural order using the Comparable interface. (11.5)
6. To enable objects to listen for action events using the ActionListener interface. (11.6)
7. To make objects cloneable using the Cloneable interface. (11.7)
8. To explore the differences between an abstract class and an interface. (11.8)
9. To create objects for primitive values using the wrapper classes Byte, Short, Integer, Long,
Float, Double, Character, and Boolean). (11.9)
10. To create a generic sort method. (11.10)
11. To simplify programming using automatic conversion between primitive types and wrapper class types.
(11.11)
12. To use the BigInteger and BigDecimal classes for computing very large numbers with arbitrarily
high precision. (11.12)
Section 11.1: Introduction
 An abstract class is a superclass that is so “abstract” it can not have any specific instances. An abstract
class is meant as a template for its subclasses rather than a class that can actually be instantiated.
 An interface is a collection of similarly-behaving methods that one wants a variety of classes to possess,
even if those classes are not in an inheritance hierarchy. An interface can not be instantiated, either; only
the classes that implement the interface (and thus all have a common method) can be instantiated (just like
only the subclasses of an abstract class can be instantiated). Interfaces are meant to give the effect of
multiple inheritance (Java only allows single inheritance).
Section 11.2: Abstract Classes

Class design should ensure that a superclass contains common features of its subclasses.
o When the common features (i.e., methods) must be implemented differently for each subclass,
then the method is declared to be abstract, which means that the subclasses must implement the
methods specified by the abstract parent class.
o The names of abstract classes and methods are italicized in UML diagrams.
o See the UML diagram below for how Lab #9: Geometric Objects in Hierarchy could be reorganized using abstract classes and methods.
Computer Science Notes
Chapter 14
Page 3 of 6
Computer Science Notes
Chapter 14
Page 4 of 6

Abstract classes are like regular classes with data fields and methods, but you can’t create an instance of
them with the new operator.

See www.cs.armstrong.edu/liang/intro7e/book/GeometricObject.java

See www.cs.armstrong.edu/liang/intro7e/book/Circle.java

See www.cs.armstrong.edu/liang/intro7e/book/Rectangle.java
Section 11.2.1: Why Abstract Methods?

Note one advantage is that since all GeometricObject2D objects are guaranteed to have a
getArea() method, your Pyramid class does not have to fool around with figuring out what type of object
the base is made of: a Rectangle or a Triangle.

See www.cs.armstrong.edu/liang/intro7e/book/TestGeometricObject.java
Section 11.2.2: Interesting Points on Abstract Classes

Abstract method can’t be in a concrete (i.e., non-abstract) class.

Abstract classes can’t be instantiated, but can still have constructors.

Abstract methods must be in abstract classes, but abstract classes don’t need to have abstract methods.

An abstract subclass can have a concrete superclass.

A subclass can override a superclass method to be abstract.

Abstract classes can be a data type.
Section 11.3: Example: Calendar and GregorianCalendar

The java.util.Date class is a basic class for representing an instant in time with millisecond
precision.

The java.util.Calendar class is an abstract class for extracting detailed calendar info about an
instant in time.

Concrete subclasses of Calendar can implement specific calendar systems, like Gregorian, lunar, or
Jewish calendars.

Currently, the only standard class supported in Java is the java.util.GregorianCalendar class.

See http://java.sun.com/javase/6/docs/api/

See http://java.sun.com/javase/6/docs/api/java/util/Calendar.html

See http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html

See www.cs.armstrong.edu/liang/intro7e/book/TestCalendar.java
Computer Science Notes
Chapter 14
Page 5 of 6
Section 11.4: Interfaces

An interface is a classlike construct that contains only constants and abstract methods.

An interface is similar to an abstract class, but its intent is to specify common behavior across objects
not in the same hierarchy.

Syntax for an interface:
modifier interface InterFaceName
{
/** Constant declarations */
/** Method signatures */
}

As with an abstract class, you cannot create an instance of an interface (e.g., with the new operator), but
an interface can be used in similar ways, like as a data type for a variable, and as the result of casting.

To state that a class you write will implement the methods in an interface, you use the keyword
implements followed by a comma-separated list of the interfaces the class implements.

See www.cs.armstrong.edu/liang/intro7e/book/TestEdible.java

See www.cs.armstrong.edu/liang/intro7e/book/Edible.java

All methods are modified as public abstract in an interface.

All data fields are modified as public final static in an interface.
o Since these must always be the modifiers, they may be omitted…

Constants can be accessed in an interface using InterfaceName.CONSTANT_NAME
Section 11.5: Example: The Comparable Interface

The Java API has an interface called Comparable that contains a single method:
compareTo(Object o)

See http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html

Classes that implement the Comparable interface can be compared and ordered using the
compareTo method.

Many classes like String and Date implement the Comparable interface.

e1.compareTo(e2) is supposed to return a negative integer, zero, or positive integer if e1 is less than,
equal to, or greater than e2 (respectively).

See www.cs.armstrong.edu/liang/intro7e/book/ComparableRectangle.java
Section 11.6: Example: The ActionListener Interface

In order for your program to respond to events like mouse clicks, right-clicks, etc., you need to:
o Include a source object in your program that will respond to an event (like a JButton object)
o create an object that handles the action event; this object must implement the
ActionListener interface and its lone method of
actionPerformed(ActionEvent e)
o register the ActionListener object with the source object using the
addActionListener(ActionListener a) method.

See www.cs.armstrong.edu/liang/intro7e/book/HandleEvent.java
Computer Science Notes
Chapter 14
Page 6 of 6
Section 11.7: Example: The Cloneable Interface

The Java API has another interface called Cloneable that simply marks whether implementing
classes can be cloned (i.e., code exists for a copy to be made) using the clone() method of the Object class.

To declare a custom class that implements the Cloneable interface, the class must overrides the clone()
method of the Object class.
o At the least, your clone() method should use super.clone(), which returns a “shallow copy” with
copies of the primitive data fields and references copied into reference fields.
o  this means that the clone’s reference fields refers to the same memory locations as the
original, and not to new memory locations…
o To perform a “deep copy”, you have to write code to create new objects for the reference data
fields…

See www.cs.armstrong.edu/liang/intro7e/book/House.java
Section 11.8: Interfaces vs. Abstract Classes

Interfaces can have constants and abstract methods only, whereas abstract classes do not have these
restrictions.

Class extension is restricted to single inheritance, but interfaces allow for multiple inheritance.

Interfaces can inherit from other interfaces using the extends keyword.

There is no root class for interfaces, unlike classes which all inherit ultimately from the Object class.
Section 11.9: Processing Primitive Data Type Values as Objects

Java has immutable “wrapper classes” built in for every primitive data type with a data field for the
primitive type, and several additional methods that do things like convert back and forth between strings and
other primitive data types.
Section 11.10: Sorting an Array of Objects

See www.cs.armstrong.edu/liang/intro7e/book/GenericSort.java
Section 11.11: Automatic Conversion between Primitive Types and Wrapper Class Types

The compiler allows you to can instantiate a wrapper class without using the standard constructor
syntax; just set the wrapper class object equal to a primitive data value.
Section 11.12: The BigInteger and BigDecimal Classes

…allow you to store arbitrarily large integers and decimal numbers…
Download