2. What You Should Learn
What You Should Learn
What is OOP?
What is OOP?
History
History
Definition
Definition
Goals
Goals
What is an Object?
What is an Object?
Definition
Definition
“
“Interface”
Interface”
“
“Class” vs. “Instance”
Class” vs. “Instance”
Three Core Principles
Three Core Principles
of OOP
of OOP
Encapsulation
Encapsulation
Inheritance
Inheritance
Polymorphism
Polymorphism
3. Why OOP? A little
Why OOP? A little
history…
history…
The “Software Crisis” (1960’s – 1980’s)
The “Software Crisis” (1960’s – 1980’s)
Computers became more powerful, and so
Computers became more powerful, and so
programs became larger and more complex.
programs became larger and more complex.
Software quality became terrible!
Software quality became terrible!
Too many bugs.
Schedules were not being met.
Difficult to add features or make changes to
software.
Existing code could not be made the building
blocks for new code – it often was easier to write
from scratch!
The field of “software engineering” was born!
The field of “software engineering” was born!
4. Software Engineering
Software Engineering
Aimed at creating high-quality software
Aimed at creating high-quality software
systems in an efficient and predictable
systems in an efficient and predictable
manner.
manner.
Abstraction was one of the prime concepts
Abstraction was one of the prime concepts
used to simplify programming problems
used to simplify programming problems
5. Abstraction
Abstraction
Abstraction is a way of dealing with things
Abstraction is a way of dealing with things
in as general a way as possible, separate
in as general a way as possible, separate
from specific details, implementations,
from specific details, implementations,
and instances.
and instances.
“
“Abstraction is a mechanism and practice to
Abstraction is a mechanism and practice to
reduce and factor out details so that one can
reduce and factor out details so that one can
focus on a few concepts at a
focus on a few concepts at a
time
time” (Wikipedia)
” (Wikipedia)
6. Abstraction – evolution
Abstraction – evolution
Procedural Programming
Procedural Programming
Routine tasks were grouped into “functions”
Routine tasks were grouped into “functions”
one function can call another function
one function can call another function
you didn't have to understand each line, just
you didn't have to understand each line, just
what each function did
what each function did
you could hide data to be accessible to only
you could hide data to be accessible to only
within a function (“encapsulation”)
within a function (“encapsulation”)
7. Abstraction – evolution
Abstraction – evolution
Structured Programming
Structured Programming
Further refinement of procedural
Further refinement of procedural
programming
programming
Formal methods of planning data-flow and
Formal methods of planning data-flow and
functional decomposition
functional decomposition
The “goto” instruction was banned
The “goto” instruction was banned
8. Abstraction – evolution
Abstraction – evolution
Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP)
Takes encapsulation even further by
Takes encapsulation even further by
localizing data and associated operations into
localizing data and associated operations into
a mini-program called an
a mini-program called an object
object.
.
An OO program is an “ecosystem” of objects
An OO program is an “ecosystem” of objects
that interact with each other.
that interact with each other.
9. What is Object Oriented
What is Object Oriented
Programming?
Programming?
“
“Think of an OO system as a bunch of
Think of an OO system as a bunch of
intelligent animals (the objects) inside
intelligent animals (the objects) inside
your machine, talking to each other by
your machine, talking to each other by
sending messages to one another.” –
sending messages to one another.” –
Allen Holub
Allen Holub
10. What is Object Oriented
What is Object Oriented
Programming?
Programming?
OOP takes abstraction furthest by
OOP takes abstraction furthest by
allowing you to group related data and
allowing you to group related data and
operations into different types of objects.
operations into different types of objects.
You no longer have to keep track of each
You no longer have to keep track of each
variable or each function, just the different
variable or each function, just the different
types of objects.
types of objects.
11. What is Object Oriented
What is Object Oriented
Programming?
Programming?
You create your own data types, which are
You create your own data types, which are
types of objects. Each of these data types
types of objects. Each of these data types
are called
are called classes
classes.
.
12. What is Object Oriented
What is Object Oriented
Programming?
Programming?
Creating your own classes allows you to
Creating your own classes allows you to
design a program so that it is intuitive to
design a program so that it is intuitive to
remember how it is organized.
remember how it is organized.
You can create classes that represent real-
You can create classes that represent real-
world business entities (e.g. Account,
world business entities (e.g. Account,
Receipt, Customer).
Receipt, Customer).
You can create classes to have specific
You can create classes to have specific
responsibilities, so that when you need to
responsibilities, so that when you need to
update a piece of code, you know exactly
update a piece of code, you know exactly
where to look for it (e.g. AccountServices).
where to look for it (e.g. AccountServices).
13. Goals of OOP
Goals of OOP
Comprehensibility - make it easier for humans
Comprehensibility - make it easier for humans
to understand the code
to understand the code
Maintainability - make code easy to modify
Maintainability - make code easy to modify
Reusability - old code should be building
Reusability - old code should be building
blocks for new code
blocks for new code
Pluggability - you can create interchangeable
Pluggability - you can create interchangeable
components that can easily substitute for one
components that can easily substitute for one
another, just like machine parts
another, just like machine parts
14. What is an Object?
What is an Object?
Has attributes
Has attributes
properties or components
properties or components
Has methods
Has methods
behaviors or routines
behaviors or routines
15. What is an Object?
What is an Object?
Ex: Car
Ex: Car
Attributes:
Attributes:
steering wheel
engine
color
radio
airconditioner
Methods:
Methods:
go forward
go backward
cool the interior
play music
16. What is an Object?
What is an Object?
Ex: Purchase Order
Ex: Purchase Order
Attributes:
Attributes:
PO Number
Buyer
Seller
List of items being
purchased
Methods:
Methods:
get PO number
get buyer
get seller
get number of items
get item number __
17. What is an Object?
What is an Object?
Ex: DB Connection
Ex: DB Connection
Attributes:
Attributes:
URL of DB
user
password
transaction isolation
level
is read-only?
(boolean)
is auto-commit?
(boolean)
Methods:
Methods:
create SQL
statement
return whether read-
only
set transaction
isolation level
close connection
set save point
rollback
18. What is an Interface?
What is an Interface?
An object has an “interface”.
An object has an “interface”.
The outward appearance of an object. How
The outward appearance of an object. How
other objects see the object.
other objects see the object.
The attributes and methods that the object
The attributes and methods that the object
exposes.
exposes.
19. What is an Interface?
What is an Interface?
Normally, an object will only expose some of
Normally, an object will only expose some of
its attributes and methods.
its attributes and methods.
Some attributes and methods are used only by
Some attributes and methods are used only by
the object itself. Therefore, no other object
the object itself. Therefore, no other object
should have access to those.
should have access to those.
Some attributes and methods may be made
Some attributes and methods may be made
accessible only to certain other objects.
accessible only to certain other objects.
Some attributes and methods may be
Some attributes and methods may be
accessible by any other object.
accessible by any other object.
20. What is an Interface?
What is an Interface?
Normally, only methods are exposed. Objects
Normally, only methods are exposed. Objects
usually hide their data to protect them from
usually hide their data to protect them from
being changed by other objects without their
being changed by other objects without their
control.
control.
Constants are an exception. These don’t
Constants are an exception. These don’t
change anyway.
change anyway.
In the case of a car (an
In the case of a car (an object
object), you can think
), you can think
of the steering wheel, pedals, and shift lever to
of the steering wheel, pedals, and shift lever to
be parts of the car's
be parts of the car's interface
interface.
.
21. What is a “Class” and an
What is a “Class” and an
“Instance”?
“Instance”?
Class – the definition of an object
Class – the definition of an object
Instance – the created object of a class
Instance – the created object of a class
22. What is a “Class” and an
What is a “Class” and an
“Instance”?
“Instance”?
Let's say you have a car, a 2004 Honda Civic
Let's say you have a car, a 2004 Honda Civic
There are many 2004 Honda Civics. “2004
There are many 2004 Honda Civics. “2004
Honda Civic” is the car type. You can think of
Honda Civic” is the car type. You can think of
this as your car's
this as your car's class.
class.
But you have one unit of 2004 Honda Civic.
But you have one unit of 2004 Honda Civic.
You can think of your car as an
You can think of your car as an instance
instance of the
of the
2004 Honda Civic class.
2004 Honda Civic class.
Each car you see can be thought of as an
Each car you see can be thought of as an
instance of the car type (class).
instance of the car type (class).
23. Three Core Principles of
Three Core Principles of
OOP
OOP
Encapsulation
Encapsulation
Inheritance
Inheritance
Polymorphism
Polymorphism
(note: different texts will have differing sets of core
(note: different texts will have differing sets of core
principles)
principles)
24. What is Encapsulation?
What is Encapsulation?
Encapsulation has two definitions:
Encapsulation has two definitions:
The grouping of data and operations into
The grouping of data and operations into
objects.
objects.
Hiding of data and operations from other
Hiding of data and operations from other
objects.
objects.
25. What is Encapsulation?
What is Encapsulation?
Grouping of data and operations into an object
Grouping of data and operations into an object
can be described as “Cohesion”.
can be described as “Cohesion”.
Related data and operations should not be
Related data and operations should not be
separated. They should be found in a single
separated. They should be found in a single
object.
object.
26. What is Encapsulation?
What is Encapsulation?
Ex: Car
Ex: Car
You shouldn’t have to ask someone with a
You shouldn’t have to ask someone with a
radar gun to measure the speed of your car.
radar gun to measure the speed of your car.
Your car should have its own speedometer to
Your car should have its own speedometer to
tell you that.
tell you that.
27. What is Encapsulation?
What is Encapsulation?
Ex: Purchase Order object
Ex: Purchase Order object
Data for purchase orders should not be
Data for purchase orders should not be
lumped in the same objects as data for
lumped in the same objects as data for
invoices and receipts.
invoices and receipts.
The methods for retrieving data from a
The methods for retrieving data from a
purchase order should not be found in a
purchase order should not be found in a
separate class from the data.
separate class from the data.
28. What is Encapsulation?
What is Encapsulation?
Ex: DB Connection object
Ex: DB Connection object
The DB Connection object should not need to
The DB Connection object should not need to
lookup the URL to the database from another
lookup the URL to the database from another
object every time it does an operation.
object every time it does an operation.
29. What is Encapsulation?
What is Encapsulation?
Hidng the data and operations from other
Hidng the data and operations from other
objects we refer to as “information hiding”.
objects we refer to as “information hiding”.
An object should expose only what is
An object should expose only what is
necessary, and only at the appropriate level.
necessary, and only at the appropriate level.
Think CIA... “need-to-know.”
Think CIA... “need-to-know.”
30. What is Encapsulation?
What is Encapsulation?
Ex: Car
Ex: Car
To driver: only steering wheel, pedals, and
To driver: only steering wheel, pedals, and
stick shift exposed. Driver should not access
stick shift exposed. Driver should not access
engine or gears or axle to drive the car.
engine or gears or axle to drive the car.
Mechanic: access to engine, gears, etc., but
Mechanic: access to engine, gears, etc., but
not internals of each part.
not internals of each part.
Manufacturer: access to internals of each
Manufacturer: access to internals of each
part.
part.
31. What is Encapsulation?
What is Encapsulation?
Ex: Purchase Order (PO) object
Ex: Purchase Order (PO) object
Any object can get info from the PO object,
Any object can get info from the PO object,
but only certain objects should have authority
but only certain objects should have authority
to set info.
to set info.
Only certain objects should be allowed to
Only certain objects should be allowed to
create the PO object.
create the PO object.
32. What is Encapsulation?
What is Encapsulation?
Ex: DB Connection object
Ex: DB Connection object
Only the Driver Manager object can create a
Only the Driver Manager object can create a
connection
connection
Users cannot set whether a connection is
Users cannot set whether a connection is
read-only or not.
read-only or not.
33. What is Encapsulation?
What is Encapsulation?
Benefits:
Benefits:
Simpler interfaces
Simpler interfaces
Only a few methods are exposed to other objects.
Since interactions between objects are simple, the
system becomes easier for the programmer to
comprehend.
Data protected from corruption
Data protected from corruption
Easier to modify code or find bugs
Easier to modify code or find bugs
Because of simpler interfaces.
34. What is Encapsulation?
What is Encapsulation?
Objects should only expose members to each
Objects should only expose members to each
other through well-defined and simple
other through well-defined and simple
interfaces.
interfaces.
Example: A driver drives a car with only
Example: A driver drives a car with only
steering wheel, pedals, gear-shift and
steering wheel, pedals, gear-shift and
dashboard meters and gauges.
dashboard meters and gauges.
Changes in one component will not affect the
Changes in one component will not affect the
others since the interfaces remain the same.
others since the interfaces remain the same.
35. Exercise
Exercise
Each team will think of a system (real-world or
Each team will think of a system (real-world or
software or whatever) and describe in detail
software or whatever) and describe in detail
the classes and instances of objects in the
the classes and instances of objects in the
system.
system.
Examples:
Examples:
traffic system at an intersection
traffic system at an intersection
payroll software
payroll software
military unit
military unit
a classroom lecture
a classroom lecture
36. Inheritance
Inheritance
A way to create a new class by “deriving” from
A way to create a new class by “deriving” from
another class.
another class.
The new class acquires the interface of the old
The new class acquires the interface of the old
class. - “Interface Inheritance”
class. - “Interface Inheritance”
The new class often also acquires the
The new class often also acquires the
implementations of the old class. -
implementations of the old class. -
“Implementation Inheritance”
“Implementation Inheritance”
The new class can change the implementations
The new class can change the implementations
of the older class or add its own methods and
of the older class or add its own methods and
attributes.
attributes.
38. Inheritance
Inheritance
You can choose to refer to a class by one of its
You can choose to refer to a class by one of its
super types if you only need the generic interface.
super types if you only need the generic interface.
You can choose to refer to a class by its specific
You can choose to refer to a class by its specific
type if you only need the specialized interface.
type if you only need the specialized interface.
39. Inheritance
Inheritance
Example of an actual class heirarchy, part of the
Example of an actual class heirarchy, part of the
Java GUI library:
Java GUI library:
Component
Button Checkbox Container TextComponent
TextArea TextField
Window
Dialog Frame
40. Inheritance
Inheritance
Inheritance is a way to allow objects to share
Inheritance is a way to allow objects to share
code, preventing
code, preventing code-duplication
code-duplication
Code-duplication is the #1 sin in OOP.
Code-duplication is the #1 sin in OOP.
41. Inheritance
Inheritance
Implementation inheritance is dangerous!
Implementation inheritance is dangerous!
The Fragile Base Class Problem
The Fragile Base Class Problem
A subclass must inherit all inheritable members of
the superclass.
No option to “disinherit” a member.
If the subclass inherits members that it doesn’t
need, those members might be called by other
objects in a way that the creator of the subclass did
not intend (remember, you’re working in a team)
causing undesirable behavior.
42. Inheritance
Inheritance
Implementation inheritance is dangerous!
Implementation inheritance is dangerous!
The Fragile Base Class Problem
The Fragile Base Class Problem
If someone modifies the superclass, the subclass
will inherit the change!
Again, this might cause undesirable behavior!
43. Inheritance
Inheritance
Implementation inheritance is dangerous!
Implementation inheritance is dangerous!
Other issues:
Other issues:
Long hierarchies become complex and difficult to
manage.
Long hierarchies lead to classes with complex
interfaces.
44. Inheritance
Inheritance
Implementation inheritance is dangerous!
Implementation inheritance is dangerous!
Prefer interface inheritance to implementation
Prefer interface inheritance to implementation
inheritance. At least you don’t inherit behavior, just the
inheritance. At least you don’t inherit behavior, just the
interface.
interface.
Never extend a class simply to save yourself some
Never extend a class simply to save yourself some
typing! There has to be a strong logical “is-a”
typing! There has to be a strong logical “is-a”
relationship between superclass and subclass.
relationship between superclass and subclass.
Otherwise, prefer
Otherwise, prefer composition
composition.
.
45. Inheritance
Inheritance
Prefer
Prefer Composition
Composition over Inheritance
over Inheritance
In
In most cases
most cases, the best way to reuse code is by
, the best way to reuse code is by
making the class you want to reuse a
making the class you want to reuse a
component (attribute) of the new class.
component (attribute) of the new class.
46. Polymorphism
Polymorphism
When a single datatype exhibits different
When a single datatype exhibits different
behaviors during execution.
behaviors during execution.
Greek: Poly means “many”, morph means
Greek: Poly means “many”, morph means
“form”. Polymorphism means “existing in
“form”. Polymorphism means “existing in
many forms”.
many forms”.
47. Polymorphism
Polymorphism
It is the other side of the same coin as
It is the other side of the same coin as
Inheritance.
Inheritance.
Polymorphism is the reason why we want to
Polymorphism is the reason why we want to
have inheritance.
have inheritance.
48. Polymorphism
Polymorphism
Polymorphism allows for “pluggability” or
Polymorphism allows for “pluggability” or
“substitutability”.
“substitutability”.
Types that implement the same interface can
Types that implement the same interface can
substitute for one another.
substitute for one another.
Client code just sees one class, but the actual
Client code just sees one class, but the actual
“concrete” type can be different in different
“concrete” type can be different in different
cases.
cases.
49. Polymorphism
Polymorphism
Method Overriding
Method Overriding
when a subclass re-implements one or more
when a subclass re-implements one or more
methods from the superclass
methods from the superclass
changes the behavior of the method
changes the behavior of the method
50. Polymorphism
Polymorphism
You can pass a subclass to a method wherever
You can pass a subclass to a method wherever
a particular class is required.
a particular class is required.
Since each subclass implements the methods
Since each subclass implements the methods
of Component differently, each subtype gets
of Component differently, each subtype gets
drawn differently, receives input differently,
drawn differently, receives input differently,
listens for different events, etc.
listens for different events, etc.
void add(Component c);
void add(Component c);
you can pass instance of Button, TextArea,
Dialog, etc, since they all inherit the interface of
Component