Engr 691-12: Special Topics in Engineering Science
Software Architecture
Fall Semester 2000
Lecture Notes

Polymorphism (Budd's UOOPJ, Ch. 12)

This is a set of "slides" to accompany chapter 12 of Timothy Budd's textbook Understanding Object-Oriented Programming with Java (Addison-Wesley, 1998).


Having, or assuming, various forms, characters, or styles (from Webster's Collegiate Dictionary, Fifth Edition).

Term used in several different ways in computer science

Polymorphism in Programming Languages

Polymorphism in Untyped Languages

Polymorphism is a trivial issue in untyped languages

All variables are potentially polymorphic

Variables take on the type of their values dynamically

silly: x
        " a silly Smalltalk polymorphic method "
        (x isKindOf: Integer) ifTrue: [ ^ x + 1  ] .
        (x isKindOf: Fraction) ifTrue: [ ^ x reciprocal  ] .
        (x isKindOf: String) ifTrue: [ ^ x reversed  ] .
        ^ nil

Forms of Polymorphism

Polymorphic Variables

Variable declared as one class can hold values from subclass


A single function name denoting two or more function bodies is overloaded

Overloading Example: Florist

class Spouse
{   public void sendFlowersTo(Address anAddress)
    {   go to florist;
        give florist message sendFlowersTo(anAddress);

class Florist
{   public void sendFlowersTo(Address anAddress);
    {   if (address is nearby)
        {   make up flower arrangement;
            tell delivery person sendFlowersTo(anAddress);
        {   look up florist near anAddress;
            contact other florist;
            give other florist message sendFlowersTo(anAddress);


Overloading and Type Signatures


Overriding occurs when child class changes meaning of function from parent

Example: Comparisons

abstract class Magnitude
{   public boolean lessOrEqual(Magnitude arg) 
            throws IncomparableMagnitudeException
    {   return (this.lessThan(arg) || this.equals(arg)); }

    public boolean greaterOrEqual(Magnitude arg) 
            throws IncomparableMagnitudeException
    {   return arg.lessOrEqual(this);  }

    public boolean lessThan(Magnitude arg)
            throws IncomparableMagnitudeException
    {   return this.lessOrEqual(arg) && ! this.equals(arg);  }

    public boolean greaterThan(Magnitude arg)
            throws IncomparableMagnitudeException
    {   return arg.lessThan(this);  }

class IncomparableMagnitudeException extends Exception
{   public IncomparableMagnitudeException() { }
    public IncomparableMagnitudeException(String msg) { super(msg); }

To define all six comparisons in a noncircular manner, subclasses must:

class Point extends Magnitude
{   public Point(int x0, int y0) { x = x0; y = y0; }

    public boolean lessThan(Magnitude arg) 
            throws IncomparableMagnitudeException
    {   if (arg instanceof Point)
        {   Point p = (Point) arg;      // less if lower left quadrant
            return x < p.x && y < p.y;  // of this point
            throw new IncomparableMagnitudeException(); 

    private int x;
    private int y;

Note: The above method uses inheritance by limitation because of the check for the Point type and throwing the exception

Deferred (Abstract) Methods

Pure Polymorphism

Generics and Templates

Review of Polymorphism in Java

Polymorphic variables? Yes

Overloading? Yes

Overriding? Yes

Deferred methods? Yes

Pure polymorphism? Yes

Generics? No

Efficiency and Polymorphism

UP to ENGR 691 Lecture Notes root document?

Copyright © 2000, H. Conrad Cunningham
Last modified: Wed Aug 30 14:15:33 2000