CSci 581: Obj.-Oriented Design & Programming
Spring Semester 1999
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).


Definition

Polymorphous:
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


Overloading

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);
        }
        else
        {   look up florist near anAddress;
            contact other florist;
            give other florist message sendFlowersTo(anAddress);
        }

    }
}


Overloading and Type Signatures


Overriding

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
        }
        else
            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 CSCI 581 Lecture Notes root document?


Copyright © 1999, H. Conrad Cunningham
Last modified: Thu Apr 22 06:56:59 CDT