M/F 2:30-3:20   
in G01 Gates Hall

CS 1130: Transition to OO Programming

Spring 2016

Part 1. More on Methods Part 3. Applications & Applets

Module 2, Part 2

More on OO Programming

In this part, we discuss a bunch of object-oriented concepts in Java.


Contents

1. Static Components

Web Lecture

Reading: Lecture Notes (Lecture Slides)
Gries/Gries, Sec. 3.3, pp. 122–124.

Comments: Only one copy of a static variable or method exists, and it resides in the class file drawer.


2. The Bottom-Up Rule

Reading: Lecture Notes
Gries/Gries, Sec. 4.1.1, pp. 143–144; Sec. 3.1.2, pp. 109–110.

Comments: The bottom-up rule (also called the method-overriding 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

Web Lecture

Reading: Lecture Notes (Lecture Slides)
Gries/Gries, Sec. 4.1.1, pp. 143–144; Sec. 3.1.2, pp. 109–110.

Comments: >Within a construct one can reference variables and methods declared in that construct and in surrounding constructs.


4. The Class Invariant

Web Lecture

Reading: Lecture Notes (Lecture Slides)
Gries/Gries, Sec. 4.1.2, pp. 146–147.

Comments: 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

Web Lecture

Reading: Lecture Notes (Lecture Slides)
Gries/Gries, Sec. 3.1.3, pp. 110–112; Sec. 4.1.3, pp. 147–148.

Comments: 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

Web Lecture

Reading: Lecture Notes
Gries/Gries, Sec. 3.1.3, pp. 110–112; Sec. 4.1.3, pp. 147–148.

Comments: If you do not declare a constructor in a class C, Java declares one for you. It looks like this: public C() { super(); }


7. Casting About

Reading: Gries/Gries, Sec. 4.2, pp. 148–153
Also watch the Widening and Narrowing lectures on p. 4-3 of the ProgramLive CD.

Comments: 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

Web Lecture

Reading: Lecture Notes (Lecture Slides)
Gries/Gries, Sec. 4.2, pp. 148–153

Comments: The apparent class of a variable is a syntactic property; its real class is a semantic property. We show how each is used.


9. Function equals

Web Lecture

Reading: Lecture Notes (Lecture Slides)
Gries/Gries, Sec. 4.3.2, pp. 154–155.

Comments: 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

Reading: Gries/Gries, Sec. 4.5, pp. 156–161.
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 another extensive example.

Comments: 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. Furthermore, make B a subclass of C if "B is a C".

Example: A dog is an animal, so make Dog a subclass of Animal.