Bject - Riented: Programming Concepts
Bject - Riented: Programming Concepts
Programming
Concepts
2000. 4. 14.
Kwang Shin Oh
Dept. of Research & Development
http://raytrust.pe. kr
[email protected]. 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
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
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
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
Object?
represent real-world objects using
software objects
also use software objects to model
abstract concepts
Visual Representation Private
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
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
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
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
Object?
encapsulation is used to hide
unimportant implementation details
from other objects
in real world
when you want to change gears on your
Object?
in software programs
you don’t need to know how a class is
implemented
you just need to know which methods to
invoke
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
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
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
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
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
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
Message?
three components are enough
information for the receiving object
to perform the desired method
No other information or context is
required
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
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
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
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
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
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
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
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
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
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
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
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,
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-
Inheritance?
object-oriented systems
take this a step further
allow classes to be defined in terms of
other classes
for example
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
34 Concepts
Object-Oriented
What is Http://raytrust.pe.kr
Inheritance?
Bicycles
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
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
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
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
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
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
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
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
Interface?
interfaces are probably most analogous
to protocols(an agreed-upon behavior)
in fact, other object-oriented
languages
have the functionality of interfaces
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
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
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
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
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
49 Concepts
Object-Oriented
Http://raytrust.pe.kr
Reference
The JavaTM Tutorial
Trail
Learning the Java Language
Lesson
Object-Oriented Programming Concepts