Held Friday, September 8, 2000
Summary
Today, we consider two important issues in object-oriented
design: inheritance and polymorphism. Both support reuse
of code, but in different ways.
Notes
- Grinnell Entrepreneurs is sponsoring a talk at 4:15 Monday, September
11th in South Lounge. The talk, entitled From a startup to
millions, Grinnell style will docus on issues in starting up a
company with close references to ASI Sign Systems, his own company. Mr.
Latimer brings much valuable insight and perspective from personal
experience. Mr. Latimer is the founder and President of ASI Sign
Systems. Mr. Latimer used to work for the college and he quit in 1985
to start a business of his own in Grinnell. The company specializes
in making signs. For eg: all the signs in the Fine Arts Building are
made by his company. The company has now grown far beyond Grinnell
and is one of the leading ones in the Midwest with offices at various
locations in the midwest.
- Are there questions on homework 2?
- Read Chapter 3 of Bailey for next week.
- Read Lab J4 for Monday.
Overview
- Inheritance
- Polymorphism
- Java's interfaces
- Inheritance and polymorphism support reuse.
- Inheritance lets you reuse and extend class definitions, creating
new classes from old.
- Polymorphism lets you reuse methods by applying them to similar
kinds of objects.
- 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
.
- Similarly, the designers of Java have said that every
Integer
is a Number
.
- How do you indicate that one class is a subclass of another? With
the
extends
keyword.
public class Circle
extends DrawableObject {
...
} // Circle
public class Integer
extends Number {
...
} // Integer
- 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();
Number n = new Integer(5);
- 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)
...
public Number add(Number val1, Number val2) {
...
} // add(Number,Number)
...
drawWithStrangeColors(new Circle());
...
Number result = add(new Integer(5), new Integer(2));
- 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
.
- Hence, every class implicitly provides all the operations of that
class.
- Good programming practice is to override all that are appropriate.
- We'll spend a few minutes looking at the design of that class.
- Its methods include
- 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.
- When you build an object in the subclass, you are implicitly
building an object in the superclass.
- So, what constructors get called?
- The rule(s) in Java:
- You build constructors for the subclass.
- Unless you specify otherwise, the zero-parameter constructor for the
superclass gets called when you construct an object in the subclass.
- To use a different constructor in the superclass, you need to write
super(parameters)
as the first line of your
class.
- We may look at some silly examples.
- 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
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,
DrawingArea sketchPad);
/** 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.
Wednesday, 23 August 2000
- Created as a blank outline.
Thursday, 24 August 2000
- Slight reorganization to page design.
Friday, 8 September 2000
- Created, based on
outline 9 of
CSC152 2000S
- Removed some uncovered parts.
- Added numeric inheritance examples.
- Added more information on
java.lang.Object.
- Added section on constructors.
- Added more information on interfaces.
- Other small udpates.
Back to Object-Oriented Design.
On to Lab: Conditionals.