object-oriented programming concepts

50
Object - Oriented Programming Concepts 2000. 4. 14. Kwang Shin Oh Dept. of Research & Development http://raytrust.pe. kr raytrust @raytrust.pe. kr Copyright 2000 © Kwang Shin Oh

Upload: kwangshin-oh

Post on 17-Nov-2014

4.402 views

Category:

Technology


2 download

DESCRIPTION

Object-Oriented Programming Concepts

TRANSCRIPT

Page 1: Object-Oriented Programming Concepts

Object - Oriented Programming

Concepts

2000. 4. 14.

Kwang Shin OhDept. of Research & Development

http://[email protected]

Copyright 2000 © Kwang Shin Oh

Page 2: Object-Oriented Programming Concepts

2Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

OO Concept if you’ve never programmed

using an object-oriented language before need to understand what objects

and classes are need to understand how these

concepts translate to code this document provides the

conceptual basis for object-oriented languages in general

Page 3: Object-Oriented Programming Concepts

3Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr What is an Object? objects are key to understanding

object-oriented technology many examples of real-world

objects your dog, your desk, your television

set, your bicycle real-world objects share two

characteristics they all have state they all have behavior

Page 4: Object-Oriented Programming Concepts

4Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Object? for example

dogs have state name, color, breed, hungry

dogs have behavior barking, fetching, slobbering

bicycles have state speed, pedal cadence, current gear

bicycles have behavior braking, changing cadence, changing

gears

Page 5: Object-Oriented Programming Concepts

5Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr What is an Object? software objects

modeled after real-world objects in that they have state and behavior

maintains its state in variables implements its behavior with

methods Definition : An object is a

software bundle of variables and related methods

Page 6: Object-Oriented Programming Concepts

6Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr What is an Object? represent real-world objects using

software objects also use software objects to

model abstract conceptsPrivateImplementationDetails

Public API

Visual Representationof a Software Object

Page 7: Object-Oriented Programming Concepts

7Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr What is an Object? software object that modeled

your real-world bicycle have variables that indicated the

bicycle’s current state its speed is 10 mph its pedal cadence is 90 rpm its current gear is the 5th gear

Page 8: Object-Oriented Programming Concepts

8Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr What is an Object? these variables and methods are

formally known as instance variables and instance methods to distinguish them from class

variables and class methods10 mphBicycle Modeled as a Software Object 90 rpm

5th gear

change cadence

change gears

bra

ke

Page 9: Object-Oriented Programming Concepts

9Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Object? anything that an object does not know

or cannot do is excluded from the object for example

bicycle (probably) doesn’t have a name, and it can’t run, bark, or fetch

thus there are no variables or methods for those states and behaviors in the bicycle class

Page 10: Object-Oriented Programming Concepts

10Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Object? the object’s variables make up the cen

ter or nucleus of the object methods surround and hide the object’

s nucleus from other objects in the program

encapsulation packaging an object’s variables within the

protective custody of its methods

Page 11: Object-Oriented Programming Concepts

11Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Object? encapsulation is used to hide unimport

ant implementation details from other objects

in real world when you want to change gears on your bi

cycle you don’t need to know how the gear mechani

sm works you just need to know which lever to move

Page 12: Object-Oriented Programming Concepts

12Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Object? in software programs

you don’t need to know how a class is implemented

you just need to know which methods to invoke

thus, the implementation details can change at any time without affecting other parts of the program

Page 13: Object-Oriented Programming Concepts

13Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Object? often, for implementation or efficiency

reasons an object may wish to expose some of its

variables or hide some of its methods so, in many languages, including Java

an object can choose to expose its variables to other objects allowing those other objects to inspect and even modify the variables

Page 14: Object-Oriented Programming Concepts

14Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Object? The Benefits of Encapsulation

Modularity the source code for an object can be written a

nd maintained independently of the source code for other obejcts

an object can be easily passed around in the system

Page 15: Object-Oriented Programming Concepts

15Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Object? Information hiding

an object has a public interface that other objects can use to communicate with it

the object can maintain private information and methods that can be changed at any time without affecting the other objects that depend on it

you don’t need to understand the gear mechanism on your bike in order to use it

Page 16: Object-Oriented Programming Concepts

16Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Message? a single object alone

not very useful usually appears as a component of a larg

er program or application that contains many other object

so, the interaction of these objects programmers achieve higher order functio

nality and more complex behavior

Page 17: Object-Oriented Programming Concepts

17Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Message? software objects interact and communi

cate with each other by sending messages to each other

when object A wants object B to perform one of B’s methods object A sends a message to object B

Object BObject A

Message

Page 18: Object-Oriented Programming Concepts

18Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Message? sometimes the receiving object needs

more information so that it knows exactly what to do this information is passed along with the

message as parameters Three components comprise a message

1. The object to whom the message is addressed2. The name of the method to perform3. Any parameters needed by the method

Page 19: Object-Oriented Programming Concepts

19Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Message? three components are enough infor

mation for the receiving object to perform the desired method

No other information or context is required

Your BicycleYou

changeGears(lowerGear)

Page 20: Object-Oriented Programming Concepts

20Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Message? The Benefits of Messages

an object’s behavior is expressed through its methods

so message passing supports all possible interactions between objects

objects don’t need to be int the same process or even on the same machine

to send and receive messages back and forth to each other

Page 21: Object-Oriented Programming Concepts

21Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? in the real world

often have many objects of the same kind for example, your bicycle is just one of many b

icycles in the world using object-oriented terminology

your bicycle obejct is an instance of the class of objects known as bicycles

Page 22: Object-Oriented Programming Concepts

22Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? bicycles have some state(speed, current cad

ence, current gear) and behavior(change gears, brake, change cadence) in common

however, each bicycle’s state is independent of and can be different fromother bicycles

produce a new blueprint for every individual bicycle they manufactured it would be very inefficient!!!!

Page 23: Object-Oriented Programming Concepts

23Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? in object-oriented software

it’s also possible to have many objects of the same kind that share characteristics

rectangles, employee records, video clips and so on

you can take advantage of the fact objects of the same kind are similar and you c

an create a blueprint for those objects

Page 24: Object-Oriented Programming Concepts

24Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? software “blueprints” for objects are ca

lled classes Definition

A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind

Page 25: Object-Oriented Programming Concepts

25Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? the values for instance variables are pr

ovided by each instance of the class so, after you’ve created the bicycle cla

ss you must instantiate it(create an instance

of it) before you can use it when you create an instance of a class

you create an object of that type

Page 26: Object-Oriented Programming Concepts

26Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? system

allocates memory for the instance variables declared by the class

then you can invoke the object’s instance methods to make it do domething

Instances of the same class share the same instance method implem

entations, which reside in the class itself

Page 27: Object-Oriented Programming Concepts

27Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? in addition to instance variables and m

ethods, classes can also define class variables and class methods

you can access class variables and methods from an instance of the class directly from a class

you don’t have to instantiate a class to use its class variables and methods

Page 28: Object-Oriented Programming Concepts

28Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? class methods can only operate on cla

ss variables they do not have access to instance varia

bles or instance methods the first time it encounters the class

the system creates a single copy of all class variables for a class

all instances of that class share its class variables

Page 29: Object-Oriented Programming Concepts

29Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? the illustrations of objects and

classes look very similar to one another

the difference between classes and objects is often the source of some confusion

in the real-world it’s obvious that classes are not themselves the

objects that they describe a blueprint of a bicycle is not a bicycle

Page 30: Object-Oriented Programming Concepts

30Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? however, in software

it’s a little more difficult to differentiate classes and objects

because software objects are merely

electronic models of real-world objects or abstract concepts in the first place

many people use the term “object” inconsistently and use it to refer to both classes and instances

Page 31: Object-Oriented Programming Concepts

31Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is a Class? The Benefit of Classes

reusability bicycle manufacturers reuse the same

blueprint over and over again to build lots of bicycles

software programmers use the same class, and thus the same code, over and over again to create many object

Page 32: Object-Oriented Programming Concepts

32Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is Inheritance? generally speaking, objects are defined

in terms of classes know a lot about an object by knowing

its class even if you don’t know what a penny-fa

rthing is if I told you it was a bicycle you would know that it had two wheels, ha

ndle bars, and pedals

Page 33: Object-Oriented Programming Concepts

33Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is Inheritance? object-oriented systems

take this a step further allow classes to be defined in terms of ot

her classes for example

mountain bikes, racing bikes, and tandems are all different kinds of bicycles

Page 34: Object-Oriented Programming Concepts

34Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is Inheritance? in object-oriented terminology

mountain bikes, racing bikes, and tandems are all subclasses of the bicycle class

similarly, the bicycle class is the superclass of mountain bikes, racing bikes, and tandems

Page 35: Object-Oriented Programming Concepts

35Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is Inheritance?

Mountain Bikes

Bicycles

Racing Bikes Tandem Bikes

Page 36: Object-Oriented Programming Concepts

36Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is Inheritance? each subclass inherits state (int the form of

variable declarations) from the superclass mountain bikes, racing bikes, and tandems shar

e some states cadence, speed, and the like

also, each subclass inherits methods from the superclass mountain bikes, racing bikes, and tandems shar

e some behaviors braking and changing pedaling speed

Page 37: Object-Oriented Programming Concepts

37Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is Inheritance? however, subclasses are not limited to

the state and behaviors provided to them by their superclass

what would be the point in that? subclasses can add variables and method

s to the ones they inherit from the superclass

for examples, tandem bicycles have two seats and two sets of handle bars

Page 38: Object-Oriented Programming Concepts

38Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is Inheritance? subclasses can also override inherited

methods and provide specialized implementations for those methods

for example if you had a mountain bike with an extra s

et of gears you would override the “change gears” me

thod so that the rider could actually use those new gears

Page 39: Object-Oriented Programming Concepts

39Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is Inheritance? not limited to just one layer of inherita

nce the inheritance tree, or class hierarchy

can be as deep as needed methods and variables are inherited do

wn through the levels in general, the further down in the hier

archy a class appears, the more specialized its behavior

Page 40: Object-Oriented Programming Concepts

40Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is Inheritance? The Benefits of Inheritance

reuse the code subclasses provide specialized behaviors fro

m the basis of common elements provided by the superclass

through the use of inheritance, programmers can reuse the code in the superclass many times

Page 41: Object-Oriented Programming Concepts

41Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.krWhat is Inheritance?

abstract classes programmers can implement superclasses cal

led abstract classes that define “generic” behaviors

the abstract superclass defines and may partially implement the behavior

but, much of the class is undefined and unimplemented

other programmers fill in the details with specialized subclasses

Page 42: Object-Oriented Programming Concepts

42Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Interface? in English

an interface is a device or system that unrelated entities use to interact

according to this definition remote control

an interface between you and television set English language

an interface between two people

Page 43: Object-Oriented Programming Concepts

43Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Interface? protocol of behavior enforced in the milita

ry an interface between people of different ranks

within the Java programming language interface

a device that unrelated objects use to interact with one another

Page 44: Object-Oriented Programming Concepts

44Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Interface? interfaces are probably most analogous to

protocols(an agreed-upon behavior) in fact, other object-oriented languages

have the functionality of interfaces but they call their interfaces protocols

Page 45: Object-Oriented Programming Concepts

45Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Interface? the bicycle class and its class hierarch

y defines what bicycles can and cannot do in terms

of its “bicycle-ness” but bicycles interact with the world on

other terms for example

a bicycle in a store is an inventory item with a retail price, a part number, a parts list, and so on

Page 46: Object-Oriented Programming Concepts

46Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Interface? to set or get this sort of information from

a bicycle object an inventory program and the bicycle class mu

st agree on a protocol of communication this protocol comes in the form of an inter

face let’s call it InventoryItem, that contains metho

d definitions the InventoryItem interface would define

methods such as setRetailPrice, getRetailPrice, and so on

Page 47: Object-Oriented Programming Concepts

47Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Interface? to work within the inventory program

the bicycle class must agree to this protocol

by implementing the InventoryItem interface when a class implements an interface

the class agrees to implement all of the methods defined in the interface

thus, the bicycle class would have to implement setRetailPrice, getRetailPrice

Page 48: Object-Oriented Programming Concepts

48Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Interface? The Benefit of Interfaces

you use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy Capturing similarities

capturing similarities between unrelated classes without artificially forcing a class relationship

Declaring methods declaring methods that one or more classes a

re expected to implement

Page 49: Object-Oriented Programming Concepts

49Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

What is an Interface? Revealing API

revealing an object’s programming interface without revealing its class

objects such as these are called anonymous objects

and can be useful when shipping a package of classes to other developers

Page 50: Object-Oriented Programming Concepts

50Object-Oriented Concepts

©2

00

0 K

wang S

hin

Oh

Http://raytrust.pe.kr

Reference The JavaTM Tutorial

TrailLearning the Java Language

LessonObject-Oriented Programming Concepts