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.
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)
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).
DefaultsClasses
In fact, these all have something in common (what?) So what is the difference could also help to define a Class.
Initialization and Constructors
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:
Things to notice:
The central ideas of OOP are
Two commonly used permissions which promote encapsulation are:
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:
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 |
Another example about who can access what (class vs instance variables and methods):
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):
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?
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):
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?
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)