Abstract Classes, Interfaces, Polymorphism, Dynamic
Binding: A Short Course
Consider ways to define the class Shape:
1. With a "default" area() method:
2. As an abstract class
public abstract class Shape
public int locationX;
public int location Y;
public abstract double area(); // note semi-colon and no method body
// can have other abstract or concrete methods...
Abstract Classes and Methods
As opposed to concrete classes and concrete
An abstract method has no method body-- just a method signature.
If a class has one or more abstract methods, it must be defined as abstract.
Programmer defines an abstract method to specify a responsibility for subclasses.
e.g., if you are a subclass of Shape, you must know how to compute your area.
class Rectangle extends Shape
The abstract method area() in Shape is necessary as it defines the responsibility of all Shape subclasses. It says that if you are a subclass of Shape, you must know how to compute area().
Why does it matter: Processing a list of "things", where each thing is special.
Iterator it = shapeList.iterator();
Shape s = (Shape) it.next();
totalArea= totalArea + s.area();
Think of a drawing editor. The user will draw shapes in random order and the program will put them in the list. So when processing each element in a loop, the programmer doesn't know the element's shape type.
Polymorphism-- An object (variable) that changes type during the execution of the program.
What object is polymorphic in the shapeList code?
Static type -- the declared type of a variable.
What is the static type of s?
Dynamic type-- the most child-like type of the
variable at run-time.
It must be the static type or a descendent.
What is the dynamic type of s?
Dynamic Binding-- Java uses the dynamic type
of an object to determine which method to call
The call to area() is really a switch statement.
Note that one can change how a list processing method works without changing the list processing method.
Restrictions on using Abstract Classes
Programmer cannot create instances of an abstract class
Shape s =
new Shape(...); // valid?
Shape s = new Rectangle(...); // valid?
Subclasses of abstract classes must define concrete methods for all abstract methods in abstract superclass,
This is not completely true. Why?
Java provides the construct "interface"
An interface, conceptually, is an abstract class with only abstract methods.
Methods in an interface are abstract by definition-- no keyword abstract required.
No concrete methods or data members, other than constants, can be defined in the interface.
Interfaces provide a third way to define Shape:
public interface Shape
// can't define locationx, locationy or any other data elements (except constants)
public float area();
public class Rectangle
public float area()
Disadvantage of using an interface is there is no place
for common methods/data.
Examples from Java library
1. ArrayList and LinkedList implement the List interface
public interface Comparable
public int compareTo(Object other)
// return value >0 means "this" greater, 0 if equal, <0 means less
Important for sorting collections (lists, trees).
1. Rewrite your Shape, Rectangle, and Circle class so that Shape is an
2. Rewrite your Shape class as an interface, and modify Rectangle and Circle appropriately.
3. Rewrite your Shape program without using inheritance (eliminate class Shape but still keep a single list of all Rect/Circles).