Outline of Class 7: Control and Inheritance
Held: Wednesday, January 28, 1998
- Tomorrow at 4:15 in Science 2424 the department will be showing
"The Proof", a film about the proof of Fermat's last theorem.
This had been a major open problem in mathematics, and was only
solved in the past few years.
- I was sorry to see so few (okay none) of you at the Haskell
discussion group. Perhaps next week?
- Don't forget that
assignment 1 is
- Turn it in with
% elm -s "Homework 1" rebelsky < scriptfile
- Any questions on assignment
- I'm hoping to have large labs ready for Friday and Monday, but we'll
see how the rest of the week goes (I didn't use similar labs last
term and this week is busy).
- At times, you want to execute a statement (or block of statements)
a fixed number of times.
- For example, to calculate the factorial of N, we might want to
multiply 1 by the numbers from one to N.
- Many programming languages provide a structure known as the
for loop for situations like this.
- Java provides a more general of the for loop. It has the form
for (initialization; test; increment)
} // for
- In effect, this is equivalent to
} // while (test)
- Any of the four parts of a
for loop can be omitted
(in which case it is ignored).
- We can then solve our initial factorial problem as follows
int i; // A counter variable
int factorial; // The factorial of N
factorial = 1;
for (i = 1; i <= N; i = i+1)
factorial = factorial * i;
} // for
- Most Java programmers increment their counter variables with
the postincrement operator,
- In many ways, Java reflects the heritage of C.
- One place that this is particularly apparent is in the preincrement
and postincrement (and pre- and post-decrement) operations.
- In C,
++ can be used before or after any variable
(or reference to a memory location). The effect is to increment
the variable by one (or an equivalent of one).
-- can be used to decrement the variable by
- In Java, you can use
-- with integer
variables (and their variants).
- If the
++ comes before the variable, the value of the
expression is the new value of the variable.
- If the
++ comes after the variable, the value of the
expression is the old value of the variable.
- At times, you need to exit your loops (or one repetition of a loop)
- For example, you might have noted an abnormal condition, or have
- While it is always possible to provide for such early exits by
intelligent use of conditionals, such conditionals can inhibit the
readability of your code.
- Hence, Java provides methods for exiting loops.
break exits the current loop (or switch).
continue exits the current repetition of the loop
(going back to the test, and, possibly, to another repetition of
- Since we often nest loops, Java also provides a mechanism for
exiting nested loops. You can label a any of the loops or
label: statement and then use
break label to break out of that statement.
for (i = 0; i < N; ++i)
for (j= 0; j < M; ++j)
"Found what we were looking for at (" +
i + "," + j + ")"
} // if
} // inner for loop
// Here's where you go if you break out of the inner loop
} // outer for loop
// Here's where you go if you break out of the outer loop
continue can take a label, and returns to the
next iteration of the labeled loop.
- 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
and refine that to particular drawable objects, such as
- How do you indicate that one class is a subclass of another? With
public class Circle
} // 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
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)
- 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
Java assumes that your object extends
- 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
- Single inheritance makes analysis and design much easier.
Otherwise, there are potential
problems with the same method name being defined in both
- Java provides interfaces to support many of
the needs of multiple inheritance.