Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 1
Objectives
To design and use abstract classes (§13.2).
To generalize numeric wrapper classes, BigInteger, and BigDecimal using the
abstract Number class (§13.3).
To specify common behavior for objects using interfaces (§13.5).
To define interfaces and define classes that implement interfaces (§13.5).
To define a natural order using the Comparable interface (§13.6).
To make objects cloneable using the Cloneable interface (§13.7).
To explore the similarities and differences among concrete classes, abstract
classes, and interfaces (§13.8).
To design the Rational class for processing rational numbers (§13.9).
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 2
Introduction
Abstract classes are like regular classes with data and
methods, but you cannot create instances of abstract
classes using the new operator.
An interface is a class-like construct that contains only
constants and abstract methods.
– Starting from Java 8, an interface can also have default methods
and static methods.
An abstract method is a method without an
implementation.
– Abstract methods are non-static.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 3
Abstract Classes and Abstract Methods
GeometricObject
Circle
Rectangle
TestGeometricObject
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 4
public abstract class GeometricObject {
private String color = "white"; Program
private boolean filled; Segment
private java.util.Date dateCreated;
……
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 5
public class TestGeometricObject {
public static void main(String[] args) {
GeometricObject geoObject1 = new Circle(5);
GeometricObject geoObject2 = new Rectangle(5, 3);
displayGeometricObject(geoObject1);
displayGeometricObject(geoObject2);
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 6
abstract method in abstract class
An abstract method cannot be contained in a
nonabstract class. If a subclass of an abstract
superclass does not implement all the abstract
methods, the subclass must be defined abstract.
In other words, in a nonabstract subclass extended
from an abstract class, all the abstract methods
must be implemented, even if they are not used in
the subclass.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 7
object cannot be created from
abstract class
An abstract class cannot be instantiated using
the new operator, but you can still define its
constructors, which are invoked in the
constructors of its subclasses.
For instance, the constructors of
GeometricObject are invoked in the Circle
class and the Rectangle class.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 8
abstract class without abstract
method
A class that contains abstract methods must be
abstract. However, it is possible to define an
abstract class that contains no abstract
methods.
In this case, you cannot create instances of the
class using the new operator. This class is used
as a base class for defining a new subclass.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 9
superclass of abstract class may be
concrete
A subclass can be abstract even if its
superclass is concrete.
For example, the Object class is concrete,
but its subclasses, such as
GeometricObject, may be abstract.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 10
concrete method overridden to be
abstract
A subclass can override a method from its
superclass to define it abstract.
This is rare, but useful when the implementation
of the method in the superclass becomes invalid
in the subclass.
In this case, the subclass must be defined
abstract.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 11
abstract class as type
You cannot create an instance from an abstract
class using the new operator, but an abstract
class can be used as a data type.
Therefore, the following statement, which
creates an array whose elements are of
GeometricObject type, is correct.
GeometricObject[] geo = new GeometricObject[10];
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 12
Case Study: the Abstract Number Class
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 13
Case Study: the Abstract Number Class
LargestNumbers
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 14
import java.util.ArrayList;
import java.math.*;
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 15
public static Number getLargestNumber(ArrayList<Number> list) {
if (list == null || list.size() == 0)
return null;
return max;
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 16
Interfaces
An interface is a classlike construct that contains only
constants and abstract methods (and default methods
and static methods starting from Java 8).
In many ways, an interface is similar to an abstract
class, but the intent of an interface is to specify
common behavior for objects.
For example, you can specify that the objects are
comparable, edible, cloneable using appropriate
interfaces.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 17
Define an Interface
To distinguish an interface from a class, Java uses the
following syntax to define an interface:
public interface InterfaceName {
constant declarations;
abstract method signatures;
}
Example:
public interface Edible {
/** Describe how to eat */
public abstract String howToEat();
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 18
Interface is a Special Class
An interface is treated like a special class in Java. Each
interface is compiled into a separate bytecode file, just
like a regular class.
Like an abstract class, you cannot create an instance
from an interface using the new operator.
You can use an interface more or less the same way you
use an abstract class.
– For example, you can use an interface as a data type for a
variable, as the result of casting, and so on.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 19
Example
You can now use the Edible interface to specify whether an object is
edible. This is accomplished by letting the class for the object
implement this interface using the implements keyword. For example,
the classes Chicken and Fruit implement the Edible interface.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 20
public interface Edible {
/** Describe how to eat */
public abstract String howToEat();
}
Edible
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 21
abstract class Animal {
private double weight;
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 22
class Chicken extends Animal implements Edible {
@Override
public String howToEat() {
return "Chicken: Fry it";
}
@Override
public String sound() {
return "Chicken: cock-a-doodle-doo";
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 23
abstract class Fruit implements Edible {
// Data fields, constructors, and methods omitted here
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 24
public class TestEdible {
public static void main(String[] args) {
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 25
Omitting Modifiers in Interfaces
All data fields are public final static and all methods are public
abstract in an interface. For this reason, these modifiers can be
omitted, as shown below:
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 26
New in Java 8
Java 8 introduced default interface methods using
the keyword default.
A default method provides a default
implementation for the method in the interface.
A class that implements the interface may simply
use the default implementation for the method or
override the method with a new implementation.
Java 8 also permits public static methods in an
interface.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 27
Example
public interface A {
public default void doSomething() {
// do something
}
public static int getValue() {
// do something
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 28
The Comparable Interface
The java.lang.Comparable interface defines
the compareTo method.
It is a generic interface. The generic type E
is replaced by a concrete type when
implementing this interface.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 29
The Comparable Interface
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 30
The Comparable Interface
Many classes in the Java library implement
Comparable.
– Byte, Short, Integer, Long, Float, Double
– Character
– BigInteger, BigDecimal
– Calendar, String, and Date
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 31
Integer and BigInteger Classes
public class Integer extends Number public class BigInteger extends Number
implements Comparable<Integer> { implements Comparable<BigInteger> {
// class body omitted // class body omitted
@Override @Override
public int compareTo(Integer o) { public int compareTo(BigInteger o) {
// Implementation omitted // Implementation omitted
} }
} }
@Override @Override
public int compareTo(String o) { public int compareTo(Date o) {
// Implementation omitted // Implementation omitted
} }
} }
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 32
Example
System.out.println(new Integer(3).compareTo(new Integer(5)));
System.out.println("ABC".compareTo("ABE"));
java.util.Date date1 = new java.util.Date(2013, 1, 1);
java.util.Date date2 = new java.util.Date(2012, 1, 1);
System.out.println(date1.compareTo(date2));
displays
-1
-2
1
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 33
The Comparable Interface
Let n be an Integer object, s be a String object, and d
be a Date object. All the following expressions are
true.
n instanceof Integer s instanceof String d instanceof java.util.Date
n instanceof Object s instanceof Object d instanceof Object
n instanceof Comparable s instanceof Comparable d instanceof Comparable
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 34
Generic sort Method
The java.util.Arrays.sort(array) method
requires that the elements in an array are
instances of Comparable<E>.
SortComparableObjects
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 35
import java.math.*;
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 37
public class ComparableRectangle extends Rectangle
implements Comparable<ComparableRectangle> {
java.util.Arrays.sort(rectangles);
for (Rectangle rectangle: rectangles) { SortRectangles
System.out.print(rectangle + " ");
System.out.println();
}
} Width: 3.4 Height: 5.4 Area: 18.36
} Width: 1.4 Height: 25.4 Area: 35.559999999999995
Width: 7.4 Height: 35.4 Area: 261.96
Width: 13.24 Height: 55.4 Area: 733.496
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 39
The Cloneable Interface
Marker Interface: An empty interface.
A marker interface does not contain constants or methods.
It is used to denote that a class possesses certain desirable
properties.
The Cloneable interface specifies that an object can be
cloned.
package java.lang;
public interface Cloneable {
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 40
The Cloneable Interface
A class that implements the Cloneable
interface is marked cloneable, and its objects
can be cloned using the clone() method
defined in the Object class.
Many classes (e.g., Date and Calendar) in the
Java library implement Cloneable. Thus, the
instances of these classes can be cloned.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 41
Examples
ArrayList<Double> list1 = new ArrayList<Double>();
list1.add(1.5);
list1.add(2.5);
list1.add(3.5);
ArrayList<Double> list2 = (ArrayList<Double>)list1.clone();
ArrayList<Double> list3 = list1;
list2.add(4.5); Output:
list3.remove(1.5); list1 is [2.5, 3.5]
System.out.println("list1 is " + list1); list2 is [1.5, 2.5, 3.5, 4.5]
List3 is [2.5, 3.5]
System.out.println("list2 is " + list2);
System.out.println("list3 is " + list3);
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 42
Examples
You can clone an array using the clone method.
Output:
int[] list1 = {1, 2};
int[] list2 = list1.clone(); list1 is 7, 2
list1[0] = 7; list2 is 1, 8
list2[1] = 8;
System.out.println("list1 is " + list1[0] + ", " + list1[1]);
System.out.println("list2 is " + list2[0] + ", " + list2[1]);
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 43
Implementing Cloneable Interface
To define a custom class that implements the
Cloneable interface, the class must override the
clone() method in the Object class.
The clone() method is declared as protected in the
Object class.
protected native Object clone() throws CloneNotSupportedException
The following code defines a class named House
that implements Cloneable and Comparable.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 44
public class House implements Cloneable, Comparable<House> {
private int id;
private double area;
private java.util.Date whenBuilt;
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 47
Shallow vs. Deep Copy
House house1 = new House(1, 1750.50);
House house2 = (House)house1.clone();
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 48
Deep Copy
To perform a deep copy, rewrite the clone()
method.
public Object clone() throws CloneNotSupportedException {
House houseClone = (House)super.clone();
houseClone.whenBuilt = (java.util.Date)(WhenBuilt.clone());
return houseClone;
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 49
Deep Copy
Alternative implementation:
public Object clone() {
try {
House houseClone = (House)super.clone();
houseClone.whenBuilt = (java.util.Date)(WhenBuilt.clone());
return houseClone;
}
catch (cloneNotSupportedException ex) {
return null;
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 50
Interfaces vs. Abstract Classes
In an interface, the data must be constants; an abstract class can
have all types of data.
Each method in an interface has only a signature without
implementation; an abstract class can have concrete methods.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 51
Interfaces vs. Abstract Classes
A class can implement multiple interfaces, but it
can only extend one superclass.
All classes share a single root, the Object class,
but there is no single root for interfaces.
Like a class, an interface also defines a type.
A variable of an interface type can reference any
instance of the class that implements the interface.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 52
Interfaces vs. Abstract Classes
An interface can inherit other interfaces using the extends
keyword. Such an interface is called a subinterface.
public interface NewInterface extends Interface1, …, InterfaceN {
…
}
A class implementing NewInterface must implement the abstract
methods defined in NewInterface, Interface1, … and
InterfaceN.
An interface can not extend classes.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 53
Interfaces vs. Abstract Classes
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 54
Whether to use an interface or a class?
In general, a strong is-a relationship that clearly describes a
parent-child relationship should be modeled using classes.
For example, a student is a person. So their relationship
should be modeled using class inheritance.
A weak is-a relationship, also known as an is-kind-of
relationship, indicates that an object possesses a certain
property. A weak is-a relationship can be modeled using
interfaces.
– For example, the String class implements the Comparable interface,
so all strings are comparable,
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 55