object-oriented programming mimi opkinscecs 277
TRANSCRIPT
OBJECT-ORIENTED PROGRAMMING
Mimi Opkins CECS 277
Objects Are Black Boxes
Code
Data
Message
Message
Objects are Building Blocks
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.
Object-Oriented Terminology
Object Inheritance
Instance Class
Behavior Methods
State Variables
Encapsulation Message
Polymorphism Superclass
State and Behavior
All real-world objects have state and behavior
A software object maintains its state in variables and its behavior in methods.
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
Object Diagram
Private
Implementation
Details
Public Interfaces
Public Interfaces
Public Interfaces Public Interfaces
Car Object Diagram
60 mphOverdriveWipers Off
Change Gears
Turn on/off wipers
Accelerate
Brake
Encapsulation
Hiding information in an object’s center and then providing a public interface for interacting with it is called encapsulation
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
Messages
Software objects interact and communicate by sending messages to each other
Rover, Speak!
Bark! Bark!
Messages With More Meaning
Back Up
changeGears(reverse)
Message Components
The object to whom the message is addressed
The name of the method to perform Any parameters needed
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
Classes
The most important feature of Object-Oriented Programming
Classes embody all features of a set of objects
Define classes of objects Software blueprint
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
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.
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
Benefits of Classes
Reusability – programmers can use the same class and therefore the same code over and over again to create different objects
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
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
Car Class Hierarchy
Car
SUV Mini Van
Sports Car
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
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
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
Comparison to Procedural Languages
Object-Oriented Programming Defines the “what” or the classes first Then the “how”
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 subtasks2. 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
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
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
Other Differences
Modules Single Entity Forced
Organization
Classes Multiple Entities Natural
Organization
Object-Oriented Design
Furry Household Pet Class
(Abstract)
Cat Class
Dog Class
Rabbit Class
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
Dragon State
Color – yellow, red, green Attitude – mean, angry, playful Appetite – full, hungry
Dragon Behavior
Get angry Calm down Eat a knight Breath fire
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
Dragon Scenario
Dragon Object IamDead
Instance Method NumberofDragons
Class Variable ReduceDragons
Class Method
Knight Object Sir Galahad
Instance Variable ChopOffDragonHe
ad Instance Method
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
Monster Class Hierarchy
Monster
Flying Monster
Swimming Monster
Flying Monster Class Hierarchy
Flying Monster
Mammal Reptile Amphibian
Four-Legged Flying Monster
Two-Legged Flying Monster
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
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
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
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
Find the Largest Commonality Between All Objects
Draw Object
All Game Pieces
Put Remaining Common Objects Together and Repeat
Draw Object
Creature Power Pieces/Bullets
Main Character
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”
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
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
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
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
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
Alien Instance Variables and Methods
Drink Beverage Level of meanness goes up by one
How Mean Am I Tell them how mean I am
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
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
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
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
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.