# Class 40: Polymorphism

Back to Inheritance. On to Polymorphism Example.

Held: Friday, 11 April 2003

Summary: Today we consider issues of polymorphism. In essence, polymorphism is the ability to use a variety of objects with similar capabilities in the same way (e.g., as parameters to the same procedure).

Related Pages:

Assignments:

Notes:

• We may have admitted students visiting today.
• Don't forget about the cool Math Education talk today at noon. Free pizza! Free extra credit!
• I was wrong. Cryptology is offered in the spring, not the fall.

Overview:

• Quick review of inheritance.
• Questions from previous class.
• Polymorphism.
• Interfaces.
• An Example.
• Abstract Classes.

## Some Fun Questions

• How does higher-orderness relate to polymorphism and and inheritance?
• Why have multiple inheritance?

## Polymorphism

• Inheritance permits subclasses to reuse the fields and methods of superclasses.
• But there are also other kinds of reuse we'd like to do.
• Suppose we've found a way to compute the smallest value in a a collection of integers.
• Can we use the same code for other purposes?
• What requirements are there to reuse the code?
• Polymorphism is the idea that an algorithm may be applied to many types of data, provided the data meet some basic criteria.
• Most typically, we say that an algorithm that applies to elements of a class also applies to elements of subclasses of that class.
• Java also supports other kinds of polymorphism.

## Interfaces

• At times, we want an object to be able to act polymorphically as many different objects.
• A `DrawableCircle` is both a circle and a drawable object.
• In some languages, we can say that a class subclasses more than one class.
• This leads to some interesting problems.
• In Java, you can only subclass one class.
• To handle the need to act like other classes, Java includes interfaces.
• An interface is a lot like a class except that it does not include definitions of the methods.
• Here's a sample interface
```public interface Drawable {
/** Clear and useful comment. */
public void drawYourself(Color baseColor,
double scaleFactor,
int top,
int left,
/** Clear and useful comment. */
public int getWidth();
/** Clear and useful comment. */
public int getHeight();
...
} // interface Drawable
```
• To say that another class acts like this interface, you would write
```public class DrawableCircle
extends Circle
implements Drawable
{
....
} // class DrawableCircle
```
• The implements is a contract. You promise to implement all the methods described in the `Drawable` interface.

## An Example

• We'll play with a `Prerequisite` class and variant issues.

## Abstract Classes

• Java provides an intermediate form between inheritance of fully-defined classes and completely undefined subclasses: abstract classes.
• You simply insert the word `abstract` as one of the adjectives describing the class (in addition to `public` and whatever else you think is appropriate).
• You can then eliminate or include as many definitions as you want.
• For the methods that subclasses should include, but aren't appropriate to define in the main class, you don't include a body.
• Java forces subclasses to implement those procedures.

## History

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Thursday, 10 April 2003 [Samuel A. Rebelsky]

• Filled in the details. Some new, some from past courses.

Friday, 11 April 2003 [Samuel A. Rebelsky]

• Filled in some more details.

Back to Inheritance. On to Polymorphism Example.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:20:59 2003.
The source to the document was last modified on Fri Apr 11 09:05:12 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.40.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu