9. • So a lot of people are complaining about OOP
• And we are still sitting here to talk about
it???
9
Read more at:
http://www.yegor256.com/2016/08/15/what-
is-wrong-object-oriented-programming.html
10. The fact is that…
• Asking 10 people about what is OOP, received 11
answers, e.g.:
– “OOP is bundling data and functions together”
– “OOP is programming with classes and inheritance”
– “OOP is all about behavior”
– “OOP is all about message passing”
– “OOP is programming with abstraction, encapsulation,
polymorphism, and inheritance”
– …
10
11. The misuse of OOP
• OOP is just a tool in your programming toolbox
• Just like any tool, it can be misused
• OOP has been misused, both in academy and
industry:
– Focus too much on class inheritance: the strongest
relationship which encourages tight-coupling
– Abuse of mutable state: object state can change over
time (with, e.g., setter methods) unnecessarily
– Use god objects which defeat the purpose of objects:
breaking-down program into small and manageable
pieces called objects
11
12. OOP is actually a good tool
• Every tool is good if you know what it is!
• That’s why we – as software developers – still
need to learn OOP, but in a correct way!
12
14. [1960s] Simula: the first OO lang
• Two Norwegians – Kristen
Nygaard and Ole-Johan Dahl –
working for the Norwegian
Defense research
• In 1960s they created Simula to
solve modeling problems:
– “They were working on simulations
that deal with exploding ships, and
realized they could group the ships
into different categories. Each ship
type would have its own class, and
the class would generate its unique
behavior and data. Simula was not
only responsible for introducing the
concept of a class, but it also
introduced the instance of a class.”
• Dahl–Nygaard Prize
14
Story: http://www.exforsys.com/tutorials/oops-concepts/the-history-of-object-oriented-programming.html
Image: http://web.cecs.pdx.edu/~black/publications/O-JDahl.pdf
15. [1970s] Smalltalk: the purist OO lang
• The term OOP was first used by
Alan Kay at Xerox PARC in their
Smalltalk language
• The term was used to refer to
the process of using objects as
the foundation for computation
• The Smalltalk team was
inspired by the Simula project,
but they designed Smalltalk so
that it would be dynamic
• Purist:
– Everything is an object
– No primitive types (int, float…)
– Message-passing style, not
method-invocation style
15
16. Since then…
1983: Bjarne
Stroustrup
ported the ideas
of Simula to C
to support
simulation
programming
“C with classes”
C++
1984: Brad Cox
added
Smalltalk-style
message-
passing syntax
to C and called
it “Objective-C”
1995: Java –
created by
James Gosling –
integrated
implementation
technology from
Smalltalk and
syntax from C++
2000: C# is the
main language
of the .NET
platform from
Microsoft, it
was created by
Anders
Hejlsberg, the
most recent
version is C# 7.1
16
Not to mention JavaScript, Ruby, Python…
17. OOP Motivation and
Core Concepts
In this section, only the core, essential, and general concepts
of OOP are discussed, language concepts are ignored
18. Behaviors and Data
• All software are about behaviors (*) operating on data,
regardless the paradigm (Procedural/OO/Functional)
• Data has concepts of:
– “type” (or “structure”)
– “value” (or “state”)
• To operate on data, a behavior must:
– understand the structure of the data
– have ability of accessing/modifying/creating/deleting the
state of the data
• Behavior A can make use of (interact with) behavior B
18
(*) you can think behaviors as functions/procedures, here I want to avoid
using such language-specific terms, the term “behaviors” is more general
19. Procedural programming (PP):
data and behaviors are separated
19
• The Person struct is
likely to be used in
many places
• Those places may
accidentally change
the state of the
person data
• When the structure
of Person changes, all
of those places are
affected
PROBLEM: data are
used in large scopes
But it’s not the fault
of PP, it’s a matter of
discipline
20. The solution/discipline of OOP
• Data must be used in a small scope called object
• Only the object has the direct access to its
(internal) data
• The outside interacts with the object A by using
the behavior exposed/provided by A
• Objects know about each other through
behaviors, and make use of those behaviors by
interacting/communicating
• When decomposing program into objects, only
behaviors matter, data don’t
20
21. OOP is all about behaviors!
• Each object has one behavior
• A behavior = a set of parameterized
operations
• An object is recognized by its behavior, not its
data
21
22. The 4 characteristics of behaviors
• the behavior of an object is its abstraction
Abstraction
• the behavior of an object encapsulates (hides) its (internal) details
Encapsulation
• behaviors are polymorphic
Polymorphism
• behaviors are inheritable
Inheritance
22
23. Abstraction
• The behavior of an object is its abstraction
• The abstraction of an object is:
– the most essential thing of that object
– the most impressive thing of that object
– stable / unlikely to change
• Finding the right abstractions (i.e., correct
behaviors) of objects is the key to programming
with OO:
– The right abs depends on the domain/app
– What if we find the wrong abs?
• Wrong abs are likely to change
23
24. Encapsulation
• The behavior of an object encapsulates (hides)
its (internal) details
• Those details contain not only data but also
everything that needs to be hidden
• Abstraction and encapsulation are very closed:
– To achieve abstraction, we need encapsulation
– When we do encapsulation, we have abstraction
• Anyway, both of them are the heart of OOP
• They are really good for managing the complicity
of software
24
25. Polymorphism
• Behaviors are polymorphic
• If an object a works with object b:
– class A {void test(b){/*work with b*/}}
– class A {
private b;
A(b) { this.b = b; }
void test() {/*work with this.b*/}
}
• Then we can pass any object c behaving like b to
a and a still works well
• Benefit: Flexibility Reusability
25
This technique is called
“dependency injection”
26. Inheritance
• Behaviors are inheritable
• If we have an object a has a behavior with 2
operations x() and y()
• Then there is a way to create an object b having
those 2 operations, and optionally with extra
customizations e.g.:
– adding operation z()
– removing operation x()
– overriding operation y()
• Benefit: Reusability
26
Not all languages
support this
27. Inheritance controversy
• In its essence, there is nothing wrong about
inheritance:
– It is a way to create objects having the same
behaviors to existing objects with optional
customizations
• So where does the controversy come from?
– Different languages have different ways to implement
object creation
– Thus, inheritance is implemented in very different
ways, some ways are even different from the essence
– Class inheritance in class-based & statically-typed
languages is the problematic one; sadly, these
languages are the most popular OOP languages
27
28. OOP key notes
• Using behaviors as the basis of abstraction is the
key of OOP.
• When a concept is abstracted and encapsulated
by a polymorphic behavior, we call it an object.
• When we program / design by defining objects,
creating objects and letting them interact in
order to achieve dedicated tasks, we call it
object-oriented programming / design.
28