Notes from Chapter 9
Objects and Classes
1. You have learned the basics of all programming languages
a. Sequence
b. Selection
c. Repetition
d. Modularity
2. Some languages are OOP (Object Oriented Programming) languages. These languages use “objects” which are combinations of data and actions. The nice thing about objects is that you can work with them as a whole rather than working on the separate pieces of data that are in the object.
What is an object?
- object = data that defines it + actions that control it
- an “object” belongs to a given “class”. For example “Rover” would be in the class “Dog”.
- class = generic way to define an object
- object = an instance of a class (an instantiation)
- examples : Class = car Object = Ford Mustang
- Class = bank account Object = your bank account
- Class = a fraction Object = 5/8
3. Example in book : a balloon
(what data describes a balloon on the screen)
an x-Coordinate, a y-Coordinate and a diameter
int
xCoord, yCoord;
int
diameter;
(what methods do you want to control its actions?)
something to change the size, something to initialize it
public
void changeSize(int change){
diameter
= diameter + change;
}
4. A class describes a balloon, but how do you define an actual balloon with some data?
- not all balloons should look the same and be placed in the same place.
- You want to define the balloons in the main program, BUT, you must have a special method in the class in order for you to do this, so let’s look at this method first. Include a method to initialize the data about a generic balloon:
public
Balloon (int d, int x, int y) {
diameter
= d;
xCoord
= x;
yCoord
= y;
}
- look at the code on page 146-147
class
Balloon{
private
int diameter;
private
int xCoord, yCoord;
public
Balloon(int d, int x, int y){
diameter
= d;
xCoord
= x;
yCoord
= y;
}
public
void changeSize(int change){
diameter
= diameter+change;
}
}
- note what is private and what is public
- note what is "inside" of what - the layout of the braces { }
5. A GENERAL RULE
You, the programmer define the class, and then you allow another user to use
the class to define objects.
However, you do not want that other user to fool around directly with the data
that he/she used to define the
particular object. Therefore you only let the other user call methods to change
things about the objects he/she
creates.
This means that when you define a class, you make the data private, but you
make the methods public.
Remember this is only a general rule.
6.1 An in class exercise: Try to define a
class describing a bank account. What data is associated with a bank account? What
actions are associated with a bank account?
6.2 An in class exercise: Try to define a class
describing a brick. What data is associated with the brick? What actions so you
want to perform on the brick?
7. OK, so you have some classes. How do you create objects from a given class?
- give your object a name
- use the Java word "new"
example: Account myChecking = new Account( );
Brick bigHeavyOne = new Brick(20,20,100,50);
These statements usually go in the init method in the main program. Let’s look at a complete example that uses the Balloon class:
import
java.applet.Applet;
import
java.awt.*;
import
java.awt.event.*;
public
class Party extends Applet implements ActionListener {
private
Button grow, shrink;
private
Balloon b1, b2;
public
void init( ){
grow
= new Button(“Grow”);
add(grow);
grow.addActionListener(this);
shrink
= new Button(“Shrink”);
add(shrink);
shrink.addActionListener(this);
b1
= new Balloon(20, 50, 50);
b2
= new Balloon(50, 70, 70);
}
public
void actionPerformed(ActionEvent e){
if(e.getSource(
) == grow) {
b1.changeSize(10);
b2.changeSize(10);
}
if(e.getSource(
) == shrink) {
b1.changeSize(-10);
b2.changeSize(-10);
}
repaint(
);
}
public
void paint(Graphics g) {
b1.display(g);
b2.display(g);
}
}
//------------------------------------------
//
NOTICE THE BREAK HERE
//------------------------------------------
class Balloon{
private
int diameter;
private
int xCoord, yCoord;
public
Balloon(int d, int x, int y){
diameter
= d;
xCoord
= x;
yCoord
= y;
}
public
void changeSize(int change){
diameter
= diameter+change;
}
public
void display(Graphics g){
g.drawOval(xCoord,
yCoord, diameter, diameter);
}
}
8. OOPS!!!! We can define a balloon, we can create a balloon, but how do we display a balloon?
- you can see the display method in the class above. Note that it is in the Balloon class. Also note that the main program calls this same method for different balloons.
- make sure it does not use any data that is not part of the class or is passed to it
- call your display method from the regular paint method of your program and
call it by using the name of your
object, then a dot, then the method name
e.g. bigHeavyOne.display(g)
- by the way, the "g", as in Graphics g is also an object, an object defined within the Java language.
9. When would you make the data items within a class public instead of private?
Almost never,
but if you have to, you could.
Example: if your class defines a piece of data as
private float accountBalance;
and if the user created an account with
Account myAccount;
then the user cannot access the balance directly.
if your class defines the data as
public float accountBalance;
then the user can access it directly by writing
myAccount.accountBalance = myAccount.accountBalance + 1000;
10. Do all methods in a class have to be public?
- No, you can make private methods. For example, if you worked at the bank that
created the Account
class, you may want to have a method that changed the interest rate for an
account. You could then change
it, but a regular customer couldn’t. However, how you really do this is a
little complicated and we won’t go
into it right now. It requires you to define other classes that have other
classes "within" them, etc.
11. You have seen the word "this" used in your programs before. What is "this"?
- "this" refers to the current object, the one being referenced "right now" in your code.
e.g. private Button go;
go = new Button("GO");
go.addActionListener(this);
Here, "this" obviously refers to the go button.
12. Some other information.
instanceof = a special Java word to check to see if a particular object is a member of a particular class.
Java has data types such as int and float, but it also has classes called Integer
and Float. The classes are
sometimes better to use because they have additional methods associated with
them, like converting a
string to an integer.
Although you used int and float (small letters), remember that when you used a
string you always capitalized
it as String. Why? Because strings are member of the class String in
Java. This means that when you want to
mess around with a string, you must call a method in the String class to do it.
Your book has a good example.
If you define
String s1 = "mom";
String s2 = "dad";
you cannot write an if statement like if ( s1 == s2 )
you must use a String method like if ( s1.equals(s2))
Look at some of the String methods available to you on page 545.
13. Methods of the same name????? Yuck!!!!
Yep, you can have two methods and they can have the same name. In OOP this is
called
"method overloading".
You could have
public blowUpBalloon ( ) {
diameter = diameter + 10;
}
or you could have
public blowUpBalloon ( int increase) {
diameter = diameter + increase;
}
How does Java know which one to call? It knows by the difference in the
parameter list.
Notice that above, one method has no parameters and one method has one.
As long as the parameter list looks different in the number and types of
parameters, Java won’t get confused.
14. The word "static"
- Do not pay much attention to this right now.
- if you define a variable within a class as "static", it means that
that variable belongs to the class,
but not to any particular objects that are instances of that class. It’s just
sort of there, if anyone needs it.
- one good use of this is to keep track of how many objects of a particular
class type were created.
See page 163.
15. Remember that you must specifically CREATE an object with the "new"
word. If you do not do this, Java
considers that your object is "null". So remember if you say
private Account myAccount;
you must also say
myAccount = new Account (2000000.00); //I just made myself rich!
16. You can define classes in separate files if you wish, but you must remember
to place them in the same
directory as your main program and you must remember to compile them separately
and you must
remember that they will go on separate .class files.
17. We need to discuss "scope" rules a little. This pertains to where
and when in a program certain variables
are accessible. For example, if you declared a variable to be private in a
class such as the Balloon class ,
you cannot expect to reference it directly from the main program.
18. Try to remember the meanings of the following words:
class, object, public, private, new, null, this, instanceof, static