6. Objects and Instances
An object is a self-contained element of a
computer program that represents a related
group of features and is designed to
accomplish specific tasks
Objects are also called instances.
8. State and Behavior
All real-world objects have state and behavior
A software object maintains its state in
variables and its behavior in methods.
9. Instance Variables and Methods
Variables and methods associated with a
particular object
An instance variable is an item of information
that defines an attribute or state
An instance method is used to implement
behavior
11. Car Object Diagram
60 mph
Overdrive
Wipers Off
Change Gears
Turn on/off wipers
Accelerate Brake
12. Encapsulation
Hiding information in an object’s center and then
providing a public interface for interacting with
it is called encapsulation
13. Benefits of Objects
Modularity – source code for an object can be
independent from the source code for other
objects
Information Hiding – private information is kept
hidden from the public interface
16. Message Components
The object to whom the message is addressed
The name of the method to perform
Any parameters needed
17. Benefits of Messages
Methods – an object’s behavior is expressed
through its methods so passing messages
supports all possible interaction between
objects
Location – objects don’t need to be in the
same process or even on the same machine to
send and receive messages
18. Classes
The most important feature of Object-Oriented
Programming
Classes embody all features of a set of objects
Define classes of objects
Software blueprint
19. Definition of Class
A class is a blueprint or prototype that defines
the variables and methods common to all
objects of certain kind.
It is a template used to create multiple objects
with similar features
20. Class Variables and Methods
A class variable is an item of information that
defines an attribute of an entire class.
Class methods implement behavior for the
entire class.
21. Static Variables and Methods
Static variables apply to the class itself and to all
of its instances
Can access static variables and methods from
an instance of the class or directly by the class
You don’t have to instantiate a class to use its
class variables and class methods
Static methods only operate on class variables
22. Benefits of Classes
Reusability – programmers can use the same
class and therefore the same code over and
over again to create different objects
23. Inheritance
Inheritance is the mechanism that enables one
class to inherit all of the state and behavior of
another class
Through inheritance, a class has all the
functionality of an existing class
A new class can be created by only indicating
how it is different from an existing class
24. Superclass and Subclass
A parent-child relationship
A class that inherits from another class is a
subclass
The class that gives the inheritance is the
superclass
26. Override
Subclasses can override inherited methods and
provide specialized implementations for those
methods
Overriding means to create a different method
definition for a method inherited from a subclass
Overriding a method can be accomplished by
passing a different number of different types of
parameters
27. Benefits of Inheritance
Specialized behavior – subclasses provide
specialized behaviors from the basis of
common variables and methods provided by
the superclass
Abstract Classes – an abstract class defines
generic behavior
Reuse – Don’t start code from scratch. Reuse
existing classes
28. Polymorphism
By inheriting its superclass’ features, an
subclass is still a member of the superclass. It
can execute all of it’s methods as well as those
of the superclass. This ability to be both a
member of subclass and a superclass is called
polymorphism.
In other words, a sports car is still a car
30. Procedure-Oriented
Programming
First you identify the task to be performed
then:
1. Top-Down Approach -- by a step-wise
refinement, break the tasks into subtasks
2. Bottom-Up Approach – write procedures to
solve simple tasks and combine them into more
sophisticated procedures
Most Programmers use a mixture of these
two approaches
31. Advantages of Object-Oriented
Programming
Classes provide a convenient clustering
mechanism for methods
The Object-Oriented structure is much
easier to grasp by the programmer or to
handle by teams of programmers
Encapsulation – classes hide their data
representation from all code except their
own classes
32. Finding a Data-Related Bug
Global
Data
function
function
function
function
function
Object Data
Object Data
Object Data
method
method
method
method
method
Procedural Approach Object-Oriented Approach
33. Other Differences
Modules
Single Entity
Forced Organization
Classes
Multiple Entities
Natural Organization
35. An Object-Oriented Program
When we write an Object-Oriented Program,
we design and construct a set of classes
When the program runs, objects are created
from those classes
We need to create the right set of classes to
accomplish what the program needs to do
36. Dragon State
Color – yellow, red, green
Attitude – mean, angry, playful
Appetite – full, hungry
38. Reasons for Calling Methods
To report a change to another object
To tell the other object to change something
about itself
To ask another object to do something
39. Dragon Scenario
Dragon Object
IamDead
Instance Method
NumberofDragons
Class Variable
ReduceDragons
Class Method
Knight Object
Sir Galahad
Instance Variable
ChopOffDragonHead
Instance Method
40. Organizing Classes
Determine if classes make up a class
hierarchy with subclasses
Functionality that is common to multiple
classes can be put in superclasses
Changes to a superclass are automatically
reflected in all their subclases
43. Steps for Object-Oriented Programming
Design
1. Break down objects to their smallest
features
2. Look for commonality between the objects
3. Look for differences between the objects
4. Find the largest commonality between all
objects
5. Put the remaining common objects together
and repeat
44. Break Down Code to its Smallest
Features
Object Feature
Creatures Location, size, power level, capability to
attack, maneuverability
Power Pieces Must be drawn, location, power level
Bullets Capability to be fired
Main Character Capability to receive commands from the
user, capability to move around the maze
according to these commands, capability to
attack, location, size
45. Look For Commonality Between the
Objects
Look for all the common relationships
between each of the objects
The main difference between the creatures and
the main character is who controls them
Have main character and creature share code
with the exception of the code to move them
46. Look For Difference Between the
Objects
Find the differences between each of the
objects
Look for relationships that unite and separate
all of the objects
The bullets move and the power pieces don’t
The computer controls creatures and the user
controls the main character
47. Find the Largest Commonality Between
All Objects
Draw Object
All Game Pieces
48. Put Remaining Common Objects
Together and Repeat
Draw Object
Creature Power Pieces/Bullets
Main Character
49. The “Big Example”
We’re going to take all the Object-Oriented
Programming and Object-Oriented Design
concepts and ideas and write a Science
Fiction Movie entitled…
“Programmers In Space”
50. Space Ship
Space Ship
Has a certain number of space shuttles
Has a certain number of imaging chambers
Has a certain number of officers
Has a certain number of aliens
Is located somewhere
Exists at a certain time
A typical Space Ship would have
Number of shuttles = 2
Location = sigma quadrant
Time period = 2253
51. Main Plot
Space Ship CECS is a new Space Ship
Number of imaging chambers = 1
Number of officers = 3
Captain = Danny
Senior Officer = Bob
Senior Officer = Jody
Number of aliens = 5
52. Humanoids
Humanoids
Have a certain number of legs
Have a certain number of arms
Have a certain number of eyes
Have a certain number of noses
Have a certain number of ears
Have a certain number mouths
Have a name
Have a certain sex
Have a favorite weapon
Have a beverage preference
53. Humanoids (continued)
A Standard Humanoid would start with
Number of legs = 2
Number of arms = 2
Number of eyes = 2
Number of noses =1
Number of ears =2
Number mouths = 1
When someone asks for your name
Tell them your name
54. Aliens
Aliens extend the idea of Humanoids
An Alien
Has non-standard ears
A certain “look”
A prophetic saying
Some level of meanness
A certain quantity of dimodium phosphate tubes
For a given Alien
It will look evil
It will start out the day calm
It will not yet have captured any dimodium phosphate tubes
55. Alien Instance Variables and
Methods
Drink Beverage
Level of meanness goes up by one
How Mean Am I
Tell them how mean I am
56. Alien Class Variables and
Methods
Prophetic Saying
Same saying for all aliens of a given species
Repeat Prophetic Saying
Retrieve it from the sacred book
57. Passing Parameters
Capture Dimodium Phosphate Tubes (# of tubes)
Increase the quantity of dimodium phosphate tubes by
the (# of tubes) amount
Gloat that you have captured (# of tubes) of dimodium
phosphate
Repeat Prophetic Saying
Retrieve it from the sacred book
58. Main Routine
There is a Space Ship called CECS
Number of imaging chambers = 1
Number of officers = 3
Captain = Danny
Senior Officer = Bob
Senior Officer = Jody
Number of aliens = 5
There is a new Human called Jennifer
Jennifer is female
Jennifer favor weapon is a keyboard
Jennifer prefers to drink coffee
59. Main Routine (continued)
There is a new Alien name Bernie
Bernie is male
Bernie has pointy ears
Bernie’s favorite weapon is a test tube
Bernie prefers to drink Pepsi
The prophetic saying spoken by Bernie’s race is “What’s
the status of that ticket?”
Bernie drinks some Pepsi
Bernie tells Jennifer how mean he is
Bernie captures 23 tubes of dimodium phosphate
Bernie quotes his prophetic saying
60. Resources for this Presentation
Campione, Mary and Walrath, Kathy. The Java Tutorial Second
Edition: Object-Oriented Programming for the Internet. Addison-
Wesley. Reading, Massachusetts. 1998.
Claerbout, Johannes. Don’t Fear the Oop!.
http://sepwww.standford.edu/sep/josman/oop/oop1.htm. 1998.
Horstmann, Cay and Cornell, Gary. Core Java 2: Volume 1 –
Fundamentals. Sun Microsoft Press. Palo Alto, California. 1999.
Lemay, Laura and Cadenhead, Rogers. Teach Yourself Java 2 in 21
Days. Sams Publishing. Indianapolis, Indiana. 1999.
Weber, Joseph L. Special Edition: Using Java 2 Platform. Que
Publishers. 1999.