Transcript Ch3
Why So Much for So Little?
• We want to use computers to solve complex
tasks.
• Complex problems require powerful
programming languages.
• Powerful programming languages have
more rules than simpler languages.
OOP with Java, David
J. Barnes
Creating and Using Objects
1
What are Object-Oriented
Languages?
• Support an O-O view of problem solving.
• Objects categorized into classes.
• Objects interact with each other.
– Objects have behavior.
– Objects maintain state.
• Behavior and state are intimately related.
• Software reuse is facilitated.
OOP with Java, David
J. Barnes
Creating and Using Objects
2
Modeling the Operation of a Port
• Classes and objects in the scenario.
–
–
–
–
–
–
The port.
Ships moving in and out.
Cargo and associated transportation.
Passengers, vehicles, car parks.
Customs officers, pilots.
Aspects of the natural environment.
OOP with Java, David
J. Barnes
Creating and Using Objects
3
Interactions in the Port
•
•
•
•
•
Transfer of cargo between ship and dock.
Passengers embarking and disembarking.
A pilot joining or leaving a ship.
A ship requesting permission to enter.
The weather affecting ship movements.
OOP with Java, David
J. Barnes
Creating and Using Objects
4
Classes and Objects
• A class definition provides a description of
a typical object within that class.
– CargoShip, PassengerFerry.
• An individual object is an instance of a
class.
• Definition of behavior and attributes.
– The attributes store an object’s state.
OOP with Java, David
J. Barnes
Creating and Using Objects
5
Class and Instances
• Liner might capture the common
characteristics of ocean liners.
– RMS Titanic and RMS Olympic might be two
instances of that class.
• Class definition is like a blueprint or
template.
– Color, size, pattern might vary, but instances of
the same class come from the same mold.
OOP with Java, David
J. Barnes
Creating and Using Objects
6
Creating an Object
// An illustration of object creation.
class ShipMain1 {
public static void main(String[] args){
// Define a method variable to refer to
// a Ship object.
Ship argo;
// Construct a new Ship object.
argo = new Ship();
}
}
OOP with Java, David
J. Barnes
Creating and Using Objects
7
Object Interactions
• Communication between people:
– Information: “I am going to a party.”
– Question: “What is the time?”
– Order/Request: “Please buy me some gum.”
• Objects communicate in similar ways.
– Passing messages to each other.
– Actor/Agent, Client/Server relationships.
OOP with Java, David
J. Barnes
Creating and Using Objects
8
Sending a Message to a Ship
// An illustration of message sending.
class ShipMain2 {
public static void main(String[] args){
// Define a method variable.
// Make it refer to a new Ship object.
Ship argo = new Ship();
// Ask the ship to report its position,
// course and speed.
argo.report();
}
}
OOP with Java, David
J. Barnes
Creating and Using Objects
9
Moving a Ship
// An illustration of ship movement.
class ShipMain3 {
public static void main(String[] args){
Ship argo = new Ship();
// Ask the ship to move.
argo.move();
}
}
OOP with Java, David
J. Barnes
Creating and Using Objects
10
Passing Arguments with
Messages
• The report and move messages convey
implicit information.
– The Ship uses its own internal state to respond.
• Some messages need to be accompanied by
further explicit information.
– “Change course to ...”
– “Reduce speed to ...”
OOP with Java, David
J. Barnes
Creating and Using Objects
11
Passing Arguments
class ShipMain4 {
public static void main(String[] args){
Ship argo = new Ship();
argo.report();
argo.move();
// Ask it to change course, move and
// report its new settings.
argo.setCourse(90);
argo.move();
argo.report();
}
}
OOP with Java, David
J. Barnes
Creating and Using Objects
12
Receiving Replies
• Human communication often requires a
reply.
– “What is the time?”
– “Are there any free berths in the port?”
– “What is the square-root of 2?”
• Similarly, we can request information from
a Ship.
• We often store the answer somewhere.
OOP with Java, David
J. Barnes
Creating and Using Objects
13
Requesting a Ship’s Speed
class ShipMain5 {
public static void main(String[] args){
Ship argo = new Ship();
argo.report();
// Define a variable to hold the speed.
double currentSpeed;
// Ask the ship what its current speed is.
currentSpeed = argo.getSpeed();
// Increase the ship's speed.
argo.setSpeed(currentSpeed+1);
argo.move();
argo.report();
}
}
OOP with Java, David
J. Barnes
Creating and Using Objects
14
Variables and Objects
• Variables and objects are different.
• A variable may refer to a single object.
• Different variables may refer to the same
object - aliases.
• A variable may be switched from one object
to another.
• A variable may be uninitialized.
OOP with Java, David
J. Barnes
Creating and Using Objects
15
The Main Method's Class
• We choose never to create an object of the
main method’s class.
• This approach is not followed by many
other authorities.
• The main method is a static method.
– Static methods have special properties that we
shall explore in due course.
OOP with Java, David
J. Barnes
Creating and Using Objects
16
Review
• O-O problem solving involves identifying
classes, objects and object interactions.
• Objects maintain state and exhibit classdefined behavior.
• Instances of the same class behave in similar
ways.
• Message passing illustrates object interaction.
OOP with Java, David
J. Barnes
Creating and Using Objects
17
Review (cont.)
• Messages may contain additional
information: in the form of arguments.
• A result may be returned as the result of a
message.
• Variables and objects are distinct.
– Variables switch between different objects.
– An object may be simultaneously referred to by
different variables.
OOP with Java, David
J. Barnes
Creating and Using Objects
18