Skip to main content

more options

Part 5. Objects and classes    Part 7. Specs, 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.


No. Topic Discussion
1. The class definition.
(blecture doc feedback)
See your first class definition, compile it, and then make it look just like a JFrame.
2. Defining methods.
(blecture doc ppt.pdf)
See how to draw an instance of a subclass, and learn how to define a function and a procedure.
  Reading for parts 1 and 2: Gries/Gries, Sec. 1.4.1, pp. 41–44.
  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.
(blecture doc ppt.pdf)
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.
(blecture doc ppt.pdf)
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.
  Reading for parts 3 and 4: Gries/Gries, Sec. 1.4.2, pp. 45–46; Sec. 3.1.3, pp. 110–112.
5. Function toString.
(blecture doc
Reading: Gries/Gries, Sec. 3.1.4, pp. 112–113.
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. On CD ProgramLive, listen to lecture The class hierarchy on page 4-2. 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 a two methods that we use often: toString and equals.
  Self-help exercise: 1. Including partition Object when drawing objects
7. Functions toString and equals in class Object, and overriding them; dealing with equality of strings.
(blecture doc ppt.pdf)
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 canot not use == to to test equality of strings —use function equals for this.
  Reading for parts 6 and 7: Gries/Gries, Sec. 4.3 introduction, p. 153; Sec. 4.3.1, p. 154; Sec. 5.2.2, p.179 only; and Sec. 4.3.2, pp. 154–155, but don't read the example.
8. Quiz 2: Objects and classes. See a consultant as soon as possible after finishing this part 6 to take a quiz. The reason for the quiz is to make sure that you understand the OO concepts. If you have misunderstandings, the consultant will help you eliminate them. You cannot take the test for module 1 until you have passed this quiz.