Held Monday, February 7, 2000
Overview
Today, we consider two important issues in object-oriented
design: inheritance and polymorphism. Both support reuse
of code, but in different ways.
Notes
- Are there any final questions on
Homework 2?
- My time was limited this weekend, so today's outline is sketchier than
normal. I apologize for the deficiency.
- Returned:
- Assignment 1: Lab J2
(I only graded the first three parts; some notes will be available tomorrow)
- Assigned:
Contents
Summary
- Goals of reuse
- Inheritance
- Overloading
- Polymorphism
- Due:
- What is object-oriented programming? You've begun to see some aspects.
- Programming with objects and classes.
- Objects join data and functionality
- Objects hide much of their internal structure, thereby separating
the ``interface'' from the ``implementation''
- But there are many other typical characteristics of object-oriented
langauges.
- The two most important ones are inheritance and
polymorphism.
- You'll find different definitions for the two, but we'll work on
some today.
- Both of these are intended to support reuse.
- If you can use the same code again and again, in a variety of
situations, you will be a more efficient programmer.
- It's also easier to update a program if you only need to
update one part.
- To continue our study of object-oriented design, we'll
spend a few minutes considering how we might build
classes that correspond to the pieces in a variety of
games.
- Othello
- Chess
- Scrabble
- Java, like most object-oriented languages, lets you subclass
existing classes to refine their functionality for particular instances.
- For example, we might have a general
DrawableObject
class
and refine that to particular drawable objects, such as Circle
or Square
.
- How do you indicate that one class is a subclass of another? With
the
extends
keyword.
public class Circle
extends DrawableObject {
...
} // Circle
- A subclass inherits all the methods and fields of its
superclass (with some restrictions due to protection mechanisms).
- That is, it can use those methods and fields without explicitly
defining or declaring them.
- A subclass may also override any of these methods by
including its own definition.
- That is, if a class defines any methods with the same name and
argument types as the superclass, then that method is used instead.
- Inheritance is used in two ways. First, it's used to automatically
provide some core functionality. In addition, it's used
guarantee the functions an object will provide. Since an object
inherits all of the ancestors methods, someone using the object knows
that they will all be available.
- Java allows you to use a member of a subclass anywhere a member
of a superclass is permitted. For example, it is perfectly valid
to write:
DrawableObject obj = new Circle();
- Similarly, if you write a function that uses a superclass, you can
pass in a member of the subclass.
public void drawWithStrangeColors(DrawableObject obj) {
...
} // drawWithStrangeColors(DrawableObject)
...
drawWithStrangeColors(new Circle());
- Java is much more restrictive on using members of superclasses in
place of members of subclasses.
- Why do you think Java has these restrictions?
- Note that if you do not provide an
extends
clause,
Java assumes that your object extends java.lang.Object
.
- Java permits only single inheritance -- a particular class
can only directly derive from one other class.
- In the alternative, multiple inheritance -- a
particular class can directly derive from more than one
other class.
- Single inheritance makes analysis and design much easier.
Otherwise, there are potential
problems with the same method name being defined in both
parent classes.
- Java provides interfaces to support many of
the needs of multiple inheritance.
- Let's try to come up with a generic
GamePiece
class
and then see how the various classes we just wrote relate
to that class.
- 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 critiera.
- 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.
- At times, we want an object to be able to act polymorphically
as many different objects.
- A
DrawawableCircle
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.
- More to come?