Objects and Classes

[Objects and Classes] [Using Methods] [Creating and using Objects] [A Class using other Classes: Picture] [Special Methods]

Objects and Classes

A class is an abstract category of things. It is a blueprint or prototype that defines the values (variables) and the actions (methods) common to all objects of a certain kind.

An object is a particular instance of a class. Software objects are often used to model real-world objects you find in everyday life.

For example:

Class
Object, or Instance
Cars The Reed family Chrysler Minivan
Songs The Star Spangled-Banner
Student Ima Hogg
Circle Blue circle in upper-left corner of screen
MemorableDate The time my car broke down

Using Methods

  1. Concepts:
    1. Transfer of control
    2. E.g. computing square of a number
  2. Why use methods?
    1. Avoid redundancy: write it only once. Makes maintenance easier, more efficient.
      1. E.g. use system's predefined methods: System.out.writeln() or Math.pow()
      2. E.g. Help organize large programs by breaking them up into logical pieces
    2. Miller's Magic number 7 +/- 2: Problem decomposition
      If this piques your interest, take a look at the original article:
      Miller, George A. 1956. The magic number seven, plus or minus two: Some limits on our capacity for processing information. Psycholgical Review 63: 81-97.

Creating and using Objects in BlueJ

It is helpful to visualize objects being created and used in BlueJ. Don't worry about understanding all the details, just get the general idea for now. We will revisit these concepts later.

Open the shapes project, and right-click to create class objects on the object bench.
(If you do not already have the BlueJ projects, you can download shapes.zip (see here for the contents of shapes.zip). Also see the downloads section of the notes and resources page if you want to download BlueJ at home.)

Call the object methods.
A class method is the name of an action associated with a class. For example, a circle could have a setColor() method, a car could have a getVIN() method, a song could have a displayAuthor() method, and a Student could have a displayBirthdate() method.
(Do a Google search on Ima Hogg...)
We have already seen the main() method. Your program can actually have many other methods as well. Methods can be used to divide up your program into meaningful pieces. For instance, you could have a method to display instructions for your program.

Call a method with parameters. Parameters are used to modify an objects properties, for example in the Circle class we have the method changeSize( int newDiameter). If we had a circle object called circle1, and we wanted to change its diameter to 25, we would write the code:
      circle1.changeSize( 25);

Note the different types of parameters. The type specifies the kind of parameter that it is, such as int (short for integer, or whole numbers), or String (a sequence of text that could include spaces, specified using "double quotes").
For instance, we can change the color of a circle. Note that the parameter list for a method could be empty, if it doesn't have any parameters.

Note that you can have multiple instances of circles, each with their own properties. These properties are more properly called fields, or variables, which store an object's state (set of properties).

A Class that uses other Classes: the Picture Class

Open up the Picture Project in BlueJ. Open the editor for the Picture and make a change.
You will need to recompile when you see the stripes under that class. (If you do not already have the BlueJ projects, you can download picture.zip or see the contents of it).

Close that project and open up the lab-classes project (If you do not already have the BlueJ projects, you can download lab-classes.zip or see the contents of it). There could be multiple instances of class Student, and there could be multiple instances of class LabClass, each of which keeps track of a list of students. (Remember this is just a taste of things to come - I don't expect you to understand all of this right now.)
See how the getName() method has a return value of type String.

Special Methods

  1. Constructor
  2. Get & Set Methods