CSCI111: Chapter 3

CSCI111: Notes Chapter 3 and 4
Using and Writing Classes and Objects

Remember that these notes are in addition to the text.
You need to read the text since it covers additional materials, particularly more use of applications.
API

Objects

An object has a state of being and behaviors it can perform to change its state of being.
Usually, computer people say an Object is a combination of some data (variables) and some actions (methods).

These say the same thing, but it is useful to think of Objects as real things when doing design. Thus variables and methods go where they are supposed to.

For example, if I were to ask you what color my cat Missy's eyes were, in what object would you look?
Me? Humans? Eyes? Color? Cats? Missy?
What is the difference between these and why would what information go where?

The following example talks about balloons. Maybe that is more interesting since it looks like your lab 2 (i.e., their balloons are circles).

The state of the circle (balloon) is its diameter and the top left of the box that perfectly encloses the balloon. Thus its Instance Variables ( as opposed to Class Variables) are:

If it were up to me, I would have had the state of the balloon (i.e., its instance variables) be its diameter and its center. Why do you suppose this example didn't do this? Graphics

A behavior (method) we would like this balloon to have is to change its size.

Objects can be Classes or Instances. Since we make Instances from Classes, in writing programs for object-oriented languages, the main thing one does is defines Classes.

Classes

This example: The declaration of the structure of all balloons is called a class

Other terms: a template, a blueprint ... an abstraction, generalization of the group

What is it that makes a balloon a balloon? What is it that makes a cat a cat (as opposed to a dog, or a human)
In fact, these all have something in common (what?) So what is the difference could also help to define a Class.

To define a Class, one wants to provide the attributes that define the object and thus provide the state of the thing (the variables) and also the behaviors that the thing is capable of (the methods).

One method that is particularly useful is how to make one of the things (an instance of the thing).

Initialization and Constructors

Defaults
What if in the definition we put the following:

This technique is done if the values are truly default values. For instance, by default, humans have two eyes. But, by default is the color blue? No, so that should not be used as a default.
Java provides the following
defaults if class variable, instance variable, or array component variables are not specified:

Constructors
Instead in the Class definition we could provide a way to construct the specific instance. Give the Class the values that this particular instance might have:

The Definition

The definition of the Class Balloon would then look like:

Things to notice:

Permissions and Scope


The use of defining classes and specifying permissions is how object-oriented languages achieve data-abstraction, encapsulation, information hiding...all of which promote modularity.

The central ideas of OOP are

  1. encapsulation
  2. inheritance
  3. polymorphism
Discuss these and overload and override. See Chapter 8 and 9 for more detail on inheritance and polymorphism (we will visit again).

Two commonly used permissions which promote encapsulation are:

  1. public
  2. private
See Table 4.5 (slide 25).

Variables defined in a method are local to the method and are not known outside of the method. This is called the variables scope. Method variables are implicitly private to the method in which they are defined.

For classes, any instance variables of the class can be "seen" by any methods of that class. However, the private variables (and methods) can only be seen (or accessed) by other methods of that same class...hence private. These would require public get and put methods for "outside" access and changes.

Public means accessible by any class

Typical order:

<access> [static]* [abstract]* ...

P's : public, private, protected, "package"

Modifier Visibility (page 148, Exploring Java; page 73 Nutshell)
public All classes
private None (only within own class)
protected Classes in package and subclasses inside or outside package
none (default) Classes in same package

Fields: Instance and Class Variables

What is the difference between a Class Variable and an Instance Variable? A class method and an instance method? (Text talks about these in Ch. 6)
Example: salary and topSalary
Color.blue and Math.PI

Another example about who can access what (class vs instance variables and methods):

Elsewhere

if a variable is an instance variable, there are copies in each object
if a variable is a class, there is only one copy

Note:

If you declare a method static, it prevents the method from accessing any variables of the class which are not also declared static (i.e., class methods cannot change instance variables):

Instantiating a class: creating a new instance

Balloon myBalloon = new Balloon(20, 50, 50);

OR

Balloon myBalloon; 
myBalloon = new Balloon(20, 50, 50); 
Each time new is used it creates a new instance of the specific class Balloon anotherBalloon = new Balloon(20, 100, 100);

What happens when this new is called?
The sequence is:

What is the difference between creation and initialization?

Displaying objects

We will need some method to display our Balloons.

You could use the method you used for lab 2, or take the easy way out (since they set us up for this):

Where did drawOval come from? ... and what is it with diameter there twice?

Notice that the Balloon class did not extends Applet. What does this mean to us?

We need a GUI of some kind - so far all we have is Applets. So...

Now, it would be nice to have buttons on our applet to make a Balloon grow or shrink.

How would one make a button?

Think about it - then look in the API. Under what?

Now what?

Things one would have to do:

Code is here: PlayBalloon.java

Look at all of code. Questions?

Here it is on the web. What would this .html page look like?

Additional information

Naming conventions

We see how to call methods. How does one access instance variables?
Is the technique different (calling methods or accessing variables) when one is in a method of the particular class?

What is the this in grow.addActionListener(this)?

How does this event-driven programming work?
and why does the instanceofmethod help me here?

What can you do with objects?
Answer: What can you do with the objects?

What if I want to tell the Class more information when I am instatiating it? Overloading?

What is null? What would the following error message mean:


Code for Ch 3 and Ch 4

Slides from text chapter 3 and chapter 4 (my notes are more on Ch 4 than Ch 3)