0% found this document useful (0 votes)
30 views

Bject - Riented: Programming Concepts

oops concepts

Uploaded by

hkgrao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Bject - Riented: Programming Concepts

oops concepts

Uploaded by

hkgrao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

O bject - O riented

Programming
Concepts
2000. 4. 14.

Kwang Shin Oh
Dept. of Research & Development

http://raytrust.pe. kr
[email protected]. kr

Copyright 2000 © Kwang Shin 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

©2000 Kwang Shin Oh


 this document provides the conceptual
basis for object-oriented languages in
general
2
Object-Oriented Concepts
What is an Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


characteristics
 they all have state
 they all have behavior
3
Object-Oriented Concepts
Http://raytrust.pe.kr

What is an Object?
 for example
 dogs have state

name, color, breed, hungry
 dogs have behavior
 barking, fetching, slobbering

©2000 Kwang Shin Oh


 bicycles have state

speed, pedal cadence, current gear
 bicycles have behavior
 braking, changing cadence, changing gears
4
Object-Oriented Concepts
What is an Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


 Definition : An object is a
software bundle of variables and
related methods
5
Object-Oriented Concepts
What is an Http://raytrust.pe.kr

Object?
 represent real-world objects using
software objects
 also use software objects to model
abstract concepts
Visual Representation Private

©2000 Kwang Shin Oh


of a Software Object Implementation
Details

Public
API
6
Object-Oriented Concepts
What is an Http://raytrust.pe.kr

Object?
 software object that modeled your
real-world bicycle
 have variables that indicated the bicycle’s
current state
 its speed is 10 mph

©2000 Kwang Shin Oh


 its pedal cadence is 90 rpm
 its current gear is the 5th gear

7
Object-Oriented Concepts
What is an Http://raytrust.pe.kr

Object?
 these variables and methods are
formally known as instance
variables and instance methods
 to distinguish them from class variables
and class methods

©2000 Kwang Shin Oh


Bicycle Modeled change gears 10 mph

as a Software Object 90 rpm


brake

5 th gear
change cadence
8
Object-Oriented Concepts
What is an Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


it can’t run, bark, or fetch
 thus there are no variables or methods for
those states and behaviors in the bicycle
class

9
Object-Oriented Concepts
What is an Http://raytrust.pe.kr

Object?
 the object’s variables make up the
center or nucleus of the object
 methods surround and hide the
object’s nucleus from other objects in
the program

©2000 Kwang Shin Oh


 encapsulation
 packaging an object’s variables within the
protective custody of its methods
10 Concepts
Object-Oriented
What is an Http://raytrust.pe.kr

Object?
 encapsulation is used to hide
unimportant implementation details
from other objects
 in real world
 when you want to change gears on your

©2000 Kwang Shin Oh


bicycle
 you don’t need to know how the gear
mechanism works

you just need to know which lever to move
11 Concepts
Object-Oriented
What is an Http://raytrust.pe.kr

Object?
 in software programs
 you don’t need to know how a class is
implemented
 you just need to know which methods to
invoke

©2000 Kwang Shin Oh


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

12 Concepts
Object-Oriented
What is an Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


 an object can choose to expose its
variables to other objects allowing those
other objects to inspect and even modify
the variables
13 Concepts
Object-Oriented
What is an Http://raytrust.pe.kr

Object?
 The Benefits of Encapsulation
 Modularity

the source code for an object can be written
and maintained independently of the source
code for other obejcts
an object can be easily passed around in the

©2000 Kwang Shin Oh


system

14 Concepts
Object-Oriented
What is an Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


depend on it

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

15 Concepts
Object-Oriented
What is a Http://raytrust.pe.kr

Message?
 a single object alone
 not very useful
 usually appears as a component of a
larger program or application that
contains many other object

©2000 Kwang Shin Oh


 so, the interaction of these objects
 programmers achieve higher order
functionality and more complex behavior

16 Concepts
Object-Oriented
What is a Http://raytrust.pe.kr

Message?
 software objects interact and
communicate with each other by
sending messages to each other
 when object A wants object B to
perform one of B’s methods

©2000 Kwang Shin Oh


 object A sends a message to object B

Message
Object A
Object B 17 Concepts
Object-Oriented
What is a Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


 Three components comprise a
message
1. The object to whom the message is addressed
2. The name of the method to perform
3. Any parameters needed by the method 18 Concepts
Object-Oriented
What is a Http://raytrust.pe.kr

Message?
 three components are enough
information for the receiving object
to perform the desired method
 No other information or context is
required

©2000 Kwang Shin Oh


changeGears(lowerGear)
You
Your Bicycle
19 Concepts
Object-Oriented
What is a Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


process or even on the same machine


 to send and receive messages back and forth
to each other

20 Concepts
Object-Oriented
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
bicycles in the world
 using object-oriented terminology

©2000 Kwang Shin Oh


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

21 Concepts
Object-Oriented
Http://raytrust.pe.kr

What is a Class?
 bicycles have some state(speed, current
cadence, current gear) and
behavior(change gears, brake, change
cadence) in common
 however, each bicycle’s state is
independent of and can be different

©2000 Kwang Shin Oh


fromother bicycles
 produce a new blueprint for every individual
bicycle they manufactured
 it would be very inefficient!!!!
22 Concepts
Object-Oriented
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

©2000 Kwang Shin Oh


 objects of the same kind are similar and you


can create a blueprint for those objects

23 Concepts
Object-Oriented
Http://raytrust.pe.kr

What is a Class?
 software “blueprints” for objects are
called classes
 Definition
A class is a blueprint or prototype
that defines the variables and

©2000 Kwang Shin Oh


methods common to all objects of a
certain kind

24 Concepts
Object-Oriented
Http://raytrust.pe.kr

What is a Class?
 the values for instance variables are
provided by each instance of the class
 so, after you’ve created the bicycle
class
 you must instantiate it(create an instance
of it) before you can use it

©2000 Kwang Shin Oh


 when you create an instance of a
class
 you

create an object of that type 25 Concepts
Object-Oriented
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

©2000 Kwang Shin Oh


 Instances of the same class
 share the same instance method
implementations, which reside in the
class itself
26 Concepts
Object-Oriented
Http://raytrust.pe.kr

What is a Class?
 in addition to instance variables and
methods, classes can also define
class variables and class
methods
 you can access class variables and
methods from

©2000 Kwang Shin Oh


 an instance of the class
 directly from a class
 you don’t have to instantiate a class
 to use its class variables and methods
27 Concepts
Object-Oriented
Http://raytrust.pe.kr

What is a Class?
 class methods can only operate on
class variables
 they do not have access to instance
variables or instance methods
 the first time it encounters the class
the system creates a single copy of all

©2000 Kwang Shin Oh


class variables for a class


 all instances of that class share its
class variables
28 Concepts
Object-Oriented
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

©2000 Kwang Shin Oh


 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
29 Concepts
Object-Oriented
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

©2000 Kwang Shin Oh


concepts in the first place
 many people use the term “object”
inconsistently and use it to refer to both
classes and instances
30 Concepts
Object-Oriented
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,

©2000 Kwang Shin Oh


and thus the same code, over and over again


to create many object

31 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

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-

©2000 Kwang Shin Oh


farthing is
 if I told you it was a bicycle
 you would know that it had two wheels,
handle bars, and pedals
32 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

Inheritance?
 object-oriented systems
 take this a step further
 allow classes to be defined in terms of
other classes
 for example

©2000 Kwang Shin Oh


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

33 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

Inheritance?
 in object-oriented terminology
 mountain bikes, racing bikes, and
tandems are all subclasses of the
bicycle class
 similarly, the bicycle class is the

©2000 Kwang Shin Oh


superclass of mountain bikes, racing
bikes, and tandems

34 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

Inheritance?
Bicycles

©2000 Kwang Shin Oh


Mountain Bikes Racing Bikes Tandem Bikes
35 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

Inheritance?
 each subclass inherits state (int the form
of variable declarations) from the
superclass
 mountain bikes, racing bikes, and tandems
share some states

cadence, speed, and the like

©2000 Kwang Shin Oh


 also, each subclass inherits methods from
the superclass
 mountain bikes, racing bikes, and tandems
share some behaviors
 braking and changing pedaling speed
36 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


methods to the ones they inherit from the
superclass
 for examples, tandem bicycles have two
seats and two sets of handle bars
37 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


set of gears
 you would override the “change gears”
method so that the rider could actually
use those new gears
38 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

Inheritance?
 not limited to just one layer of
inheritance
 the inheritance tree, or class hierarchy
 can be as deep as needed
 methods and variables are inherited

©2000 Kwang Shin Oh


down through the levels
 in general, the further down in the
hierarchy a class appears, the more
specialized its behavior
39 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

Inheritance?
 The Benefits of Inheritance
 reuse the code

subclasses provide specialized behaviors
from the basis of common elements provided
by the superclass
through the use of inheritance, programmers

©2000 Kwang Shin Oh


can reuse the code in the superclass many


times

40 Concepts
Object-Oriented
What is Http://raytrust.pe.kr

Inheritance?
 abstract classes
 programmers can implement superclasses
called abstract classes that define “generic”
behaviors
 the abstract superclass defines and may
partially implement the behavior

©2000 Kwang Shin Oh



but, much of the class is undefined and
unimplemented

other programmers fill in the details with
specialized subclasses

41 Concepts
Object-Oriented
What is an
Http://raytrust.pe.kr

Interface?
 in English
 an interface is a device or system that
unrelated entities use to interact
 according to this definition
 remote control

©2000 Kwang Shin Oh


 an interface between you and television set
 English language
 an interface between two people

42 Concepts
Object-Oriented
What is an
Http://raytrust.pe.kr

Interface?
 protocol of behavior enforced in the
military
 an interface between people of different
ranks
 within the Java programming
language

©2000 Kwang Shin Oh


 interface
 a device that unrelated objects use to interact
with one another
43 Concepts
Object-Oriented
What is an
Http://raytrust.pe.kr

Interface?
 interfaces are probably most analogous
to protocols(an agreed-upon behavior)
 in fact, other object-oriented
languages
 have the functionality of interfaces

©2000 Kwang Shin Oh


 but they call their interfaces protocols

44 Concepts
Object-Oriented
What is an
Http://raytrust.pe.kr

Interface?
 the bicycle class and its class
hierarchy defines
 what bicycles can and cannot do in terms
of its “bicycle-ness”
 but bicycles interact with the world on
other terms

©2000 Kwang Shin Oh


 for example
 a bicycle in a store is an inventory item with a
retail price, a part number, a parts list, and so
on
45 Concepts
Object-Oriented
What is an
Http://raytrust.pe.kr

Interface?
 to set or get this sort of information from
a bicycle object

an inventory program and the bicycle class
must agree on a protocol of communication
 this protocol comes in the form of an
interface

©2000 Kwang Shin Oh



let’s call it InventoryItem , that contains
method definitions
 the InventoryItem interface would
define methods such as setRetailPrice,
getRetailPrice, and so on
46 Concepts
Object-Oriented
What is an
Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


 the class agrees to implement all of the
methods defined in the interface
 thus, the bicycle class would have to
implement setRetailPrice, getRetailPrice
47 Concepts
Object-Oriented
What is an
Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


relationship
 Declaring methods

declaring methods that one or more classes
are expected to implement
48 Concepts
Object-Oriented
What is an
Http://raytrust.pe.kr

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

©2000 Kwang Shin Oh


of classes to other developers

49 Concepts
Object-Oriented
Http://raytrust.pe.kr

Reference
 The JavaTM Tutorial
 Trail
Learning the Java Language

Lesson
Object-Oriented Programming Concepts

©2000 Kwang Shin Oh


50 Concepts
Object-Oriented

You might also like