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

CS 1130: Transition to OO Programming

Spring 2016

Part 5. Objects and Classes Part 7. Specifications, Testing, and Debugging

Module 1, Part 6

The Class Definition

You will see your first class definition and learn about subclasses and superclasses. You will also see definitions of methods —functions, procedures, and constructors— and variables, called fields, in a class definition.


Contents

1. The Class Definition

Web Lecture

Reading: Lecture Notes
Gries/Gries, Sec. 1.4.1, pp. 41–44.

Comments: See your first class definition, compile it, and then make it look just like a JFrame.


2. Defining Methods

Web Lecture

Reading: Lecture Notes (Lecture Slides)
Gries/Gries, Sec. 1.4.1, pp. 41–44.

Comments: See how to draw an instance of a subclass, and learn how to define a function and a procedure.

Self-help Exercises: These are important, do them all!
1. Drawing objects of subclasses
2. Writing your own subclass of JFrame


3. Declaring Fields and Getter/Setter Methods

Web Lecture

Reading: Lecture Notes
Gries/Gries, Sec. 1.4.2, pp. 45–46; Sec. 3.1.3, pp. 110–112.

Comments: See how to declare fields in a class. Usually, they are made private, so they cannot be referenced directly from outside the class. But getter/setter methods can be provided to allow access.


4. The Constructor and the new-Expression

Web Lecture

Reading: Lecture Notes (Lecture Slides)
Gries/Gries, Sec. 1.4.2, pp. 45–46; Sec. 3.1.3, pp. 110–112.

Comments: We need a simple way to initialize fields of a newly created object. The constructor, a new kind of method, is the vehicle for this. After describing the constructor, we visit the new-expression once again and show you how to execute it.


5. Function toString

Web Lecture

Reading: Lecture Notes
Gries/Gries, Sec. 3.1.4, pp. 112–113.

Sample Code:

  • Chapter.java
  • Point.java
  • Comments: A Java convention is to write a function toString in (almost) every class that will yield a description of the object in which it resides. It is useful especially when debugging a program.


    6. The Class Hierarchy

    Reading: ProgramLive lecture 4-2 (The class hierarchy)
    Gries/Gries, Sec. 4.3 and 4.3.1, pp. 153-154. Also Sec. 5.2.2, p.179 only; and Sec. 4.3.2, pp. 154–155, (but do not read the example)

    Comments: Java has a built-in class Object, and every class you write that does not extend a class automatically extends class Object. If we draw a tree (drawn with the root at the top) showing the superclass-subclass structure, Object is at the root of the tree, so it is the superest class of them all. It has two methods that we use often: toString and equals.

    Self-help Exercise: Including Partition Object when Drawing Objects


    7. Overriding toString and equals

    Web Lecture

    Reading: Lecture Notes (Lecture Slides)
    Gries/Gries, Sec. 4.3 and 4.3.1, pp. 153-154. Also Sec. 5.2.2, p.179 only; and Sec. 4.3.2, pp. 154–155, (but do not read the example)

    Comments: We present the specs of functions toString and equals in class Object and talk about overriding these functions. We make a special point to discuss the meaning of b == c when b and c have class-types. And we discuss why one cannot not use == to to test equality of strings —use function equals for this.