• Abstract properties from several kinds of related things
  • Create a class that has the common properties
  • Triangle, Rectangle, Line, Circle
  • Generalize to Shape
  • Common properties are position, outline color, fill color


  • Create a specialized version of a class
  • Example: subclass of Application for a particular application


  • Create a subclass with restricted properties
  • Example
    • Start with general list
    • Limit to stack or queue


  • Define a common set of properties
  • Example: Comparator
  • In Java, usually use interfaces


  • Add new behavior to a class
  • Java LinkedHashMap extends HashMap


  • Combine characteristics of two or more classes
  • Requires multiple inheritance or composition


  • Composition is building a new class with fields that are existing objects
  • Objects of the two classes have a has-a relationship


  • Just inheritance
  • Objects of the two classes have an is-a relationship

Stack Via Composition

import java.util.*;

public class CompositionStack<T> extends ArrayList<T> {
  private ArrayList<T> items;

  public CompositionStack() {
    items = new ArrayList<T>();
  public void push(T item) {
  public T pop() {
    T item = items.get(items.size() - 1);
    items.remove(items.size() - 1);
    return item;

Stack Via Construction

import java.util.*;

public class ConstructionStack<T> extends ArrayList<T> {
  public void push(T item) {
  public T pop() {
    T item = get(size() - 1);
    remove(size() - 1);
    return item;

Construction vs Composition

  • With composition can allow a limited collection of methods
  • Java has no way of reducing visibility of fields or methods in subclasses
  • With construction there is less code to write


  • The same function can take arguments of different types
  • Substitutability: an object of a subclass type can substitute for an argument of the superclass type
  • Dynamic binding - the type of an object is determined at runtime rather than compile time


  • Different functions with the same name but different signatures
  • The signature is the name, list of argument types, and return type


  • A subclass redefines a method
  • The method that is called is determined at runtime (dynamic binding)


  • Implicit type conversion
  • Example: short to int
  • Widening (promotion)
  • Narrowing
  • Boxing
  • Unboxing