Impact of Polymorphism on Program Enhancements
OOPSLA97 Workshop:
Resources for Early Object Design Education
TITLE:
Impact of Polymorphism on Program Enhancements
RESOURCE CONTACT:
Ed C. Epp
Mathematics and Computer Science Department
University of Portland
5000 N. Willamette Blvd.
Portland, OR 97203
epp@uofport.edu
GOAL:
- Concepts - to understand inheritance, polymorphism, dynamic binding
- Design - to understand how inheritance affects the relationships between
objects in a hierarchical system
- Implementation - ability to write code that implements inheritance
- Maintenance - ability to modify existing code that utilizes inheritance
AUDIENCE:
End of first semester or Beginning of second semester of a CS1/2 sequence.
PREREQUISITES:
- Understanding of object use
- Ability to implement classes
- Elementary notion of inheritance desirable
MOTIVATION:
This exercise is designed to show that enhancing a system utilizing inheritance
makes some problems easier to understand, implement, and enhance.
APPLICABILITY:
Inheritance is a difficult idea to understand, and therefore it is often
difficult to motivate students to incorporate inheritance in their programs.
Ability to use inheritance requires an understanding of concept as well
as experience. Concepts are readily introduced in lecture. The experience
is gained in a laboratory setting.
STRUCTURE:
Students are given two systems to enhance: one using inheritance and
the other not. They record the effort required to modify each system. Since,
the function of this activity is to demonstrate the utility of inheritance,
a software system is chosen that benefits from inheritance.
CONSEQUENCES:
The implementation using inheritance is expected to be easiest to modify.
Some students may believe it is easier to understand because there is less
code. Other students may believe it is more difficult to understand because
inheritance, polymorphism, and dynamic binding are new ideas.
IMPLEMENTATION:
- Software components
This exercise focuses on two implementations of a drawing program. Diagrams
are created in a display by selecting one of several drawing tools from
a tool palette, for example: rectangle, line, or star. Once the tool is
selected, a component is drawn using point and drag.
One of the implementations does not use inheritance. It utilizes a list
of glyphs. Each glyph has a tag field that specifies whether it is a rectangle,
line, or star. The paint method iterates through the list and, based on
the components tag, calls the appropriate paint for rectangle, line, or
star.
The second implementation uses inheritance. It utilizes a glyph class which
is inherited by rectangle, line, and star. Polymorphism is used to store
all three types in a single list. Dynamic binding is used to call paint
without needing to determine what kind of glyph is being used.
- Activity
Students are required to add an oval glyph to each implementation of draw.
They must record each modification made: where the modifications were made,
as well as the code that achieved the modification.
- Analysis
Students rate the effort required for each modification by assigning x
points for each new statement and y points for each method (or other location)
requiring modifications. In addition, students provide subjective observations
about the process of enhancing these programs. These figures, along with
observations, are used evaluate which design is easiest to maintain.
REFERENCES:
Patterns in Computer Science Using Java
by Ed. C. Epp
To be published by McGraw Hill.