Lecture 6
Lecture 6
• Interfaces
• The Comparable Interface
2
Abstract Classes
• An abstract class can contain abstract methods, which are implemented in concrete subclasses.
• Classes become more specific and concrete with each new subclass.
• If you move from a subclass back up to a superclass, the classes become more general and less
specific.
• Class design should ensure that a superclass contains common features of its subclasses.
............
/** Abstract
public method
abstract getArea
double */
getArea();
in the subclass.
6
Abstract Classes
• 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.
7
Example
public class TestClass {
public static void main(String[] args) {
// Create two geometric objects
GeometricObject Object1 = new Circle(5);
GeometricObject Object2 = new Rectangle(5, 3);
// Display objects
displayGeometricObject(Object1);
displayGeometricObject(Object2);
}
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.
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.
11
abstract class as type
• You cannot create an instance from an abstract class using the new operator,
• Therefore, the following statement, which creates an array whose elements are
of GeometricObject type, is correct.
• You can then create an instance of GeometricObject and assign its reference to the array
like this:
12
Case Study: the Abstract Number Class
• The Number class is an
abstract superclass for
Double, Float, Long, Integer,
Short, Byte, BigInteger and
BigDecimal.
13
import java.util.ArrayList;
import java.math.*;
public class LargestNumbers { Example
public static void main(String[] args)
{ ArrayList<Number> list = new ArrayList<>();
list.add(45); // Add an integer
list.add(3445.53); // Add a
double
// Add a BigInteger
list.add(new
BigInteger("3432323234344343101"))
;
// Add a BigDecimal
} list.add(new BigDecimal("2.0909090989091343433344343"));
public static Number getLargestNumber(ArrayList<Number>
System.out.println("The largest number is " + list) {
if (list == null || list.size() ==
getLargestNumber(list));
0) return null;
Number number = list.get(0);
for (int i = 1; i < list.size(); i++)
if (number.doubleValue() < list.get(i).doubleValue())
number = list.get(i);
return number;
14
}
Interfaces
What is an interface?
15
Interfaces
• An interface is containing only constants and Syntax:
abstract methods. Access_modifier interface InterfaceName{
• Like an abstract class, you cannot create an instance from an interface using
the new operator,
• In most cases 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.
17
Example
• The Edible interface is used to
specify whether an object is edible.
interface.
21
The Comparable Interface
• The Comparable interface defines the
compareTo method for comparing objects.
public interface Comparable<E> {
• The interface is defined in java.lang
public int compareTo(E o);
package. }
22
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
1
24
Generic sort Method
import java.math.*;
public class SortComparableObjects {
public static void main(String[] args) {
String[] cities = {"Savannah", "Boston", "Atlanta", "Tampa"};
java.util.Arrays.sort(cities); The java.util.Arrays.sort
for (String city: cities)
System.out.print(city + " "); (array) method requires
System.out.println();
that the elements in an
BigInteger[] hugeNumbers = {new BigInteger("2323231092923992"),
new BigInteger("432232323239292"),array are instances of
new BigInteger("54623239292")};
java.util.Arrays.sort(hugeNumbers);
Comparable<E>.
for (BigInteger number: hugeNumbers)
System.out.print(number + " ");
}
25
}
Defining Classes to Implement
Comparable
• You cannot use the sort method to sort an array of Rectangle objects, because Rectangle
does not implement Comparable.
• However, you can define a new rectangle class that implements Comparable.
26
Defining Classes to Implement
Comparable
public class ComparableRectangle extends Rectangle implements Comparable<ComparableRectangle> {
/** Construct a ComparableRectangle with specified properties */
public ComparableRectangle(double width, double height) {
super(width, height);
}
//@Override // Implement the compareTo method defined in Comparable
public int compareTo(ComparableRectangle o) {
if (getArea() > o.getArea())
return 1;
else if (getArea() < o.getArea())
return -1;
else
return 0;
}
• 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.
29
Assignment
• Design a Triangle class that extends the abstract GeometricObject class. Draw the UML diagram
for the classes Triangle and GeometricObject and then implement the Triangle class.
• Write a test program that prompts the user to enter three sides of the triangle, a color, and a
Boolean value to indicate whether the triangle is filled.
• The program should create a Triangle object with these sides and set the color and filled
properties using the input.
• The program should display the area, perimeter, color, and true or false to indicate whether it
is filled or not.
30