Skip to main content

more options

Part 1. More on methods    Part 3. Applications & applets

Module 2, part 2. More on OO

Introduction

We discuss a bunch of ideas in OO in Java.

Contents

No. Topic Discussion
1. Static components,
(blecture doc ppt.pdf)
Reading: Gries/Gries, Sec. 3.3, pp. 122–124.
Only one copy of a static variable or method exists, and it resides in the class file drawer.
2. The bottom-up rule, also called the method-overriding rule.
(html)
The bottom-up rule describes how to find a variable, or a method with a certain signature, in an object. Because of the way we draw objects, use of this rule always finds the overriding method, if a method is inherited.
3. The inside-out rule,
(blecture doc ppt.pdf)
Within a construct one can reference variables and methods declared in that construct and in surrounding constructs.
Reading for items 2 and 3.
Gries/Gries, Sec. 4.1.1, pp. 143–144; Sec. 3.1.2, pp. 109–110.
4. The class invariant.
(blecture doc ppt.pdf)
Reading: Gries/Gries, Sec. 4.1.2, pp. 146–147.
The set of constraints given in the documentation of the fields of a class constitute the class invariant. We show why you should be careful in writing and using it.
5. Calling constructors from constructors.
(blecture doc ppt.pdf)
Within a constructor, one can call a constructor in the same class or in the superclass. Such a call must be the first statement of the constructor body.
6. The default constructor.
(blecture doc)
If you don't declare a constructor in a class C, Java declares one for you. It looks like this: public C() {super();} .
Reading for items 5 and 6.
Gries/Gries, Sec. 3.1.3, pp. 110–112; Sec. 4.1.3, pp. 147–148.
7. Casting about. Watch the Widening and Narrowing lectures on p. 4-3 of the ProgramLive CD. We extend the notion of narrower and wider types to class types and show the consequences thereof. We also introduce operator instanceof.
8. Apparent and real classes.
(blecture doc ppt.pdf)
The apparent class of a variable is a syntactic property; its real class is a semantic property. We show how each is used.
Reading for items 7 and 8.
Gries/Gries, Sec. 4.2, pp. 148–153.
9. Function equals.
(blecture doc ppt.pdf)
Reading: Gries/Gries, Sec. 4.3.2, pp. 154–155.
Function equals is defined in class Object, the superest class of them all. So it is inherited by all classes. We show you the convention for overriding it in a subclass.
10. Object-oriented design. Lesson page 3-8 of ProgramLive contains general discussion of OO design with classes and an extensive example.

Lesson page 4-4 extends this to a discussion of the use of subclasses and gives an extensive example.

Reading: Gries/Gries, Sec. 4.5, pp. 156–161.
We make a first attempt at discussing how to organize a program into classes and subclasses. This is only an introduction to the topic.

Generally, nouns of the problem domain become classes, while verbs become method names.

Make B a subclass of C if "B is a C". Example: A dog is an animal, so make Dog a subclass of Animal.