object-oriented programming mimi opkinscecs 277

60
OBJECT-ORIENTED PROGRAMMING Mimi Opkins CECS 277

Upload: albert-ray

Post on 23-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

OBJECT-ORIENTED PROGRAMMING

Mimi Opkins CECS 277

Page 2: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277
Page 3: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Objects Are Black Boxes

Code

Data

Message

Message

Page 4: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Objects are Building Blocks

Page 5: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277
Page 6: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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.

Page 7: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Object-Oriented Terminology

Object Inheritance

Instance Class

Behavior Methods

State Variables

Encapsulation Message

Polymorphism Superclass

Page 8: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

State and Behavior

All real-world objects have state and behavior

A software object maintains its state in variables and its behavior in methods.

Page 9: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 10: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Object Diagram

Private

Implementation

Details

Public Interfaces

Public Interfaces

Public Interfaces Public Interfaces

Page 11: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Car Object Diagram

60 mphOverdriveWipers Off

Change Gears

Turn on/off wipers

Accelerate

Brake

Page 12: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Encapsulation

Hiding information in an object’s center and then providing a public interface for interacting with it is called encapsulation

Page 13: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 14: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Messages

Software objects interact and communicate by sending messages to each other

Rover, Speak!

Bark! Bark!

Page 15: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Messages With More Meaning

Back Up

changeGears(reverse)

Page 16: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Message Components

The object to whom the message is addressed

The name of the method to perform Any parameters needed

Page 17: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 18: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Classes

The most important feature of Object-Oriented Programming

Classes embody all features of a set of objects

Define classes of objects Software blueprint

Page 19: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 20: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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.

Page 21: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 22: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Benefits of Classes

Reusability – programmers can use the same class and therefore the same code over and over again to create different objects

Page 23: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 24: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 25: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Car Class Hierarchy

Car

SUV Mini Van

Sports Car

Page 26: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 27: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 28: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 29: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Comparison to Procedural Languages

Object-Oriented Programming Defines the “what” or the classes first Then the “how”

Page 30: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 31: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 32: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 33: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Other Differences

Modules Single Entity Forced

Organization

Classes Multiple Entities Natural

Organization

Page 34: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Object-Oriented Design

Furry Household Pet Class

(Abstract)

Cat Class

Dog Class

Rabbit Class

Page 35: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 36: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Dragon State

Color – yellow, red, green Attitude – mean, angry, playful Appetite – full, hungry

Page 37: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Dragon Behavior

Get angry Calm down Eat a knight Breath fire

Page 38: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 39: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Dragon Scenario

Dragon Object IamDead

Instance Method NumberofDragons

Class Variable ReduceDragons

Class Method

Knight Object Sir Galahad

Instance Variable ChopOffDragonHe

ad Instance Method

Page 40: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 41: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Monster Class Hierarchy

Monster

Flying Monster

Swimming Monster

Page 42: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Flying Monster Class Hierarchy

Flying Monster

Mammal Reptile Amphibian

Four-Legged Flying Monster

Two-Legged Flying Monster

Page 43: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 44: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 45: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 46: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 47: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Find the Largest Commonality Between All Objects

Draw Object

All Game Pieces

Page 48: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Put Remaining Common Objects Together and Repeat

Draw Object

Creature Power Pieces/Bullets

Main Character

Page 49: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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”

Page 50: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 51: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 52: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 53: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 54: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 55: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

Alien Instance Variables and Methods

Drink Beverage Level of meanness goes up by one

How Mean Am I Tell them how mean I am

Page 56: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 57: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 58: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 59: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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

Page 60: OBJECT-ORIENTED PROGRAMMING Mimi OpkinsCECS 277

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.