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