Académique Documents
Professionnel Documents
Culture Documents
Chapter 8
Object-Based Programming
(Not Responding)
• That message means that some Windows object is
not responding to messages.
• Some program called a method, but Windows
failed to respond. (No surprise)
Class
MyClass boop;
At this point, the reference called
“boop” does not actually point to any
existing object.
Soon, it will point to an object of type
MyClass, but now the object doesn’t exist.
What?
How?
Who?
Java I—Copyright © 2000 Tom Hunter
OOP Vocabulary
state behavior identity
• Key characteristics:
(What) What is the object’s state?
(How) What is the object’s behavior?
(Who) What is the object’s identity?
’598-22-7893’,’$10.00’,’22.25’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee marie;
marie = new HourlyEmployee(‘Mary’,’J.’,
’555-24-1516’,’$30.00’,’0’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee marie;
marie = new HourlyEmployee(‘Mary’,’J.’,
’555-24-1516’,’$30.00’,’0’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
marie.setSSN( ‘444-33-1264’ );
ClassName.primitive_variable
ClassName.Object_variable.
use
containment ( “has-a” )
inheritance ( “is-a” )
Use
Containment ( “has-a” )
inheritance ( “is-a” )
• A method of Order :
creates
receives or
returns
objects of class Account .
• Design Tip:
Avoid the “use” relationship whenever
you can. If you “use” somebody else’s class,
then any changes to that class can break your
class.
Account
Order message
Order
Account
Java I—Copyright © 2000 Tom Hunter
OOP Vocabulary
Relationships Between Classes: containment
use
“has-a”
“is-a”
Java I—Copyright © 2000 Tom Hunter
Techniques
for
Using Objects
JLabel howdy;
howdy = new JLabel( “How Are Ya?” );
hello = howdy;
• Now, both references point to the exact same object.
howdy = hello;
out1 = howdy.getText();
out2 = hello.getText();
out1 = howdy.getText();
out2 = hello.getText();
H O W E V E R...
javac -d Time2.java
public Time1()
{
setTime( 0, 0, 0 );
}
public Time1()
{
setTime( 0, 0, 0 );
} In keeping with encapsulation, the member-
accessvoid
public modifiers
setTime(declare ourm,instance
int h, int int s ) variables
{ private.
hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
When
}
this class gets instantiated, the only way
second = ( ( s >= 0 && s < 60 ) ? s : 0 );
to
access these variables is through the methods of
the class.
Java I—Copyright © 2000 Tom Hunter
import java.text.DecimalFormat;
public Time1()
{
setTime( 0, 0, 0 );
}
( More Later )
Java I—Copyright © 2000 Tom Hunter
public String toUniversalString()
Method toString() originates in class Object. When you
{
DecimalFormat
want to see what twoDigits = new DecimalFormat(
is in the instance variables of the "00"
class,);you
call the toString() method. Every class you create
return twoDigits.format( hour ) + ":" +
shouldtwoDigits.format(
override this method and )create
minute + ":"its+ own
copy.
twoDigits.format( second );
}
System.exit( 0 );
}
} Java I—Copyright © 2000 Tom Hunter
import javax.swing.JOptionPane;
System.exit( 0 );
}
} Java I—Copyright © 2000 Tom Hunter
import javax.swing.JOptionPane;
SavingsAccount.interestRate
SavingsAccount.getInterestRate()
SavingsAccount.getInterestRate()
ClassName.staticMethod( parameters );
variables and
methods