Abstract classes and abstract methods Chapter 11 Lectures created by Prof. Michael Overton Modified by Professor Sana Odeh.

Download Report

Transcript Abstract classes and abstract methods Chapter 11 Lectures created by Prof. Michael Overton Modified by Professor Sana Odeh.

Abstract classes and abstract methods
Chapter 11
Lectures created by Prof. Michael Overton
Modified by Professor Sana Odeh
Abstract Classes and Interfaces in Java
•
•
•
•
•
Abstract classes
Interfaces
The Comparable interface
Event listeners
All in chapter 10: please read it
Abstract classes and abstract methods
• Abstract methods define a method
signature but nothing else
• They must be implemented in
subclasses
• If a class has an abstract method, it
must also be abstract
Example from chapter 10
[public] abstract class GeometricObject{
private String name;
private String dateCreated;
private Color color;
[public] abstract double getArea();
// don’t know how to compute it until we know what kind
// of object it is. Details should be provided in specialized class
[public] abstract double getPerimeter();
// same story
}
Implementation is in subclass
[public] class Circle extends GeometricObject{
private double radius;
…
public double getArea(){
return radius*radius*Math.PI;
}
}
Points to note
• You cannot construct an object of an abstract class
• Instead, you construct an object of one of the
subclasses that implements the abstract methods
• Do not use the keyword implements
• It isn’t needed because the subclass must implement
the abstract methods of the superclass– unless it is
also abstract
• An abstract class can still have constructors, which
can then be invoked using super in the subclasses
(constructor chaining)
Points to note, continued
• An abstract class can be used as a datatype, for
example:
• GeometricObject[] geoObjs = new
GeometricObject[10];
• Here it is not the geometric object that is being
constructed with “new”, but the array of references to
possible future GeometricObjects
Interfaces
• An interface is like an abstract class, but different.
• It contains only constants and abstract methods
• This is different from an abstract class which can have data
fields and concrete (non-abstract) methods
• You cannot construct an interface object.
• Instead you construct objects of classes that implement the
interface
• These classes are not subclasses of the interface, so the
keyword implements is used
• You cannot construct an interface object
• However, you can declare variables to have a type interface, and
such a variable can reference any instance of a class that
implement the interface
The Comparable interface
• Here is the declaration in java.lang:
public interface Comparable{
public int compareTo(Object o);
}
• The documentation explains that compareTo should
return -1 if the implicit parameter object is “less
than” the explicit parameter object, +1 if it is
“greater than”, and 0 if it is “equals”
• It also “strongly recommends” that it should return 0
only in the case that the equals method would
returns true for the same pair of objects
• Actually any int value can be returned; only the sign
is relevant.
Implementing Comparable
• in GeometricObject
• in Date (instead of “later”)
A Max Function
//returns the “bigger” of 2 objects
public static Comparable maxObj(Comparable o1,
Comparable o2){
if(o1.compareTo(o2)>= 0){
return o1;
}
return o2;
}