Object-Oriented
Programming

MELJUN CORTES

Java Fundamentals and Object
Oriented Programming
The Complete Java Boot Camp
...
What You Should Learn
What is OOP?

I.
A.
B.
C.

History
Definition
Goals

What is an Object?

II.
A.
B.
C.

Definition
“I...
Why OOP? A little
history…
 The “Software Crisis” (1960’s – 1980’s)
 Computers became more powerful, and so programs
bec...
Software Engineering
 “creating high-quality software systems in

an efficient and predictable manner”

 Abstraction was...
Abstraction
 Wikipedia: “abstraction is a mechanism

and practice to reduce and factor out details
so that one can focus ...
Abstraction – evolutions
 Procedural Programming


routines were grouped into “functions”



one function can call anot...
Abstraction – evolutions
 Structured Programming


further refinement of procedural
programming



formal methods of pl...
Abstraction – evolutions
 Object-Oriented Programming


Takes encapsulation even further by
localizing data and associat...
What is Object Oriented
Programming?
 “Think of an OO system as a bunch of

intelligent animals (the objects) inside
your...
What is Object Oriented
Programming?
 OOP takes abstraction furthest by

allowing you to group related data and
operation...
What is Object Oriented
Programming?
 You create your own data types, which are

types of objects. Each of these data typ...
What is Object Oriented
Programming?
 Creating your own classes allows you to

design a program so that it is intuitive t...
Goals of OOP
 Comprehensibility - make it easier for humans
to understand the code

 Maintainability - make code easy to...
What is an Object?
 Has attributes


properties or components

 Has methods
 behaviors or routines
What is an Object?
Ex: Car
 Attributes:






steering wheel
engine
color
radio
airconditioner

 Methods:
 go forw...
What is an Object?
Ex: Purchase Order
 Attributes:





PO Number
Buyer
Seller
List of items being
purchased

 Metho...
What is an Object?
Ex: DB Connection
 Attributes:







URL of DB
user
password
transaction isolation
level
is rea...
What is an Interface?
 An object has an “interface”.

The outward appearance of an object. How
other objects see the obje...
What is an Interface?
 Normally, an object will only expose some of its

attributes and methods.
 Some attributes and me...
What is an Interface?
 Normally, only methods are exposed.

Objects usually hide their data to protect
them from being ch...
What is a “Class” and an
“Instance”?
 My car is a ’92 Nissan Sentra. That is it’s

class. There are many other ’92 Nissan...
What is a “Class” and an
“Instance”?
 Class – the definition of an object
 Instance – the created object of a class
Three Core Principles of
OOP
 Encapsulation
 Inheritance
 Polymorphism
(note: different texts will have differing sets ...
What is Encapsulation?
Encapsulation has two definitions:
 The grouping of data and operations into
objects.
 Hiding of ...
What is Encapsulation?
 The first definition can be described as

“Cohesion”.
 Related data and operations should not be...
What is Encapsulation?
Ex: Car
 You shouldn’t have to ask someone with a
radar gun to measure the speed of your
car. Your...
What is Encapsulation?
Ex: Purchase Order object
 Data for purchase orders should not be
lumped in the same objects as da...
What is Encapsulation?
Ex: DB Connection object
 The DB Connection object should not
need to lookup the URL to the databa...
What is Encapsulation?
 The other definition can be defined as

“information hiding”.
 An object should expose only what...
What is Encapsulation?
Ex: Car
 To driver: only steering wheel, pedals, and stick
shift exposed. Driver should not access...
What is Encapsulation?
Ex: Purchase Order object
 Any object can get info from the object,
but only certain objects shoul...
What is Encapsulation?
Ex: DB Connection object
 Only the Driver Manager object can create
a connection
 Users cannot se...
What is Encapsulation?
Benefits:
 Simpler interfaces


Only a few methods are exposed to other
objects. Since interactio...
What is Encapsulation?
 Objects should only expose members to each

other through well-defined and simple interfaces.
 E...
Inheritance
 A way to create a new class by “deriving”

from another class.

The new class acquires the interface of the ...
Inheritance
 Subclasses become “sub-types” of the super

classes.
Inheritance
 You can choose to refer to a class by one of its

super types if you only need the generic
interface.
 You ...
Inheritance
 Example of an actual class heirarchy, part

of the Java GUI library:
Component

Button

Checkbox

Container
...
Inheritance
 Inheritance is a way to allow objects to

share code, preventing code-duplication.


Code-duplication is th...
Inheritance
 Implementation inheritance is dangerous!
 The Fragile Base Class Problem


A subclass must inherit all inh...
Inheritance
 Implementation inheritance is dangerous!


The Fragile Base Class Problem




If someone modifies the sup...
Inheritance
 Implementation inheritance is dangerous!


Other issues:




Long hierarchies become complex and difficul...
Inheritance
 Implementation inheritance is dangerous!

Prefer interface inheritance to implementation
inheritance. At lea...
Polymorphism
 When a single datatype exhibits different

behaviors during execution.
 Greek: Poly means “many”, morph me...
Polymorphism
 It is the other side of the same coin as

Inheritance.
 Polymorphism is the reason why we want
to have inh...
Polymorphism
 Polymorphism allows for “pluggability” or

“substitutability”.
Types that implement the same interface can
...
Polymorphism
 Method Overriding

when a subclass re-implements one or more
methods from the superclass
 changes the beha...
Polymorphism
 You can pass a subclass to a method

wherever a particular class is required.
void add(Component c);
 you ...
Polymorphism
 Since each subclass implements the methods of

Component differently, each subtype gets drawn
differently, ...
T he End
Java Fundamentals and Object-Oriented
Programming
The Complete Java Boot Camp
Upcoming SlideShare
Loading in …5
×

MELJUN CORTES Java Lecture OOP

373 views

Published on

MELJUN CORTES Java Lecture OOP

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
373
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
26
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Example: Inheritance in java.awt package
  • MELJUN CORTES Java Lecture OOP

    1. 1. Object-Oriented Programming MELJUN CORTES Java Fundamentals and Object Oriented Programming The Complete Java Boot Camp MELJUN CORTES
    2. 2. What You Should Learn What is OOP? I. A. B. C. History Definition Goals What is an Object? II. A. B. C. Definition “Interface” “Class” vs. “Instance” Three Core Principles of OOP III. A. B. C. Encapsulation Inheritance Polymorphism
    3. 3. 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 – easier to write from scratch!  The field of “software engineering” was born!
    4. 4. Software Engineering  “creating high-quality software systems in an efficient and predictable manner”  Abstraction was one of the prime concepts used to simplify programming problems
    5. 5. Abstraction  Wikipedia: “abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time”
    6. 6. Abstraction – evolutions  Procedural Programming  routines 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”)
    7. 7. Abstraction – evolutions  Structured Programming  further refinement of procedural programming  formal methods of planning data-flow and functional decomposition  “goto” banned
    8. 8. Abstraction – evolutions  Object-Oriented Programming  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.
    9. 9. 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.” – Allan Holub
    10. 10. 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.
    11. 11. What is Object Oriented Programming?  You create your own data types, which are types of objects. Each of these data types are called “classes”.
    12. 12. 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. For example, you can create classes that represent real-world business entities.  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. 
    13. 13. 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 substitute for one another, just like machine parts
    14. 14. What is an Object?  Has attributes  properties or components  Has methods  behaviors or routines
    15. 15. What is an Object? Ex: Car  Attributes:      steering wheel engine color radio airconditioner  Methods:  go forward  go backward  cool the interior  play music
    16. 16. What is an Object? Ex: Purchase Order  Attributes:     PO Number Buyer Seller List of items being purchased  Methods:  get PO number  get buyer  get seller  get number of items  get item number __
    17. 17. What is an Object? Ex: DB Connection  Attributes:       URL of DB user password transaction isolation level is read-only? (boolean) is auto-commit? (boolean)  Methods:  create SQL statement  return whether readonly  set transaction isolation level  close connection  set save point  rollback
    18. 18. 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. 
    19. 19. 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.
    20. 20. 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.
    21. 21. What is a “Class” and an “Instance”?  My car is a ’92 Nissan Sentra. That is it’s class. There are many other ’92 Nissan Sentras out there.  But there is only one instance of my car.
    22. 22. What is a “Class” and an “Instance”?  Class – the definition of an object  Instance – the created object of a class
    23. 23. Three Core Principles of OOP  Encapsulation  Inheritance  Polymorphism (note: different texts will have differing sets of core principles)
    24. 24. What is Encapsulation? Encapsulation has two definitions:  The grouping of data and operations into objects.  Hiding of data and operations from other objects.
    25. 25. What is Encapsulation?  The first definition can be described as “Cohesion”.  Related data and operations should not be separated. They should be found in a single object.
    26. 26. 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.
    27. 27. 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.
    28. 28. 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.
    29. 29. What is Encapsulation?  The other definition can be defined as “information hiding”.  An object should expose only what is necessary, and only at the appropriate level.  Think CIA... “need-to-know”.
    30. 30. 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.
    31. 31. What is Encapsulation? Ex: Purchase Order object  Any object can get info from the object, but only certain objects should have authority to set info.  Only certain objects should be allowed to create the PO object.
    32. 32. 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.
    33. 33. What is Encapsulation? Benefits:  Simpler interfaces  Only a few methods are exposed to other objects. Since interactions between objects are simple, system is easier for the programmer to comprehend.  Data protected from corruption  Easier to modify code or find bugs  Because of simpler interfaces.
    34. 34. 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.
    35. 35. 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. 
    36. 36. Inheritance  Subclasses become “sub-types” of the super classes.
    37. 37. 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.
    38. 38. Inheritance  Example of an actual class heirarchy, part of the Java GUI library: Component Button Checkbox Container TextComponent Window Dialog TextArea Frame TextField
    39. 39. Inheritance  Inheritance is a way to allow objects to share code, preventing code-duplication.  Code-duplication is the #1 sin in OOP.
    40. 40. 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.
    41. 41. 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!
    42. 42. Inheritance  Implementation inheritance is dangerous!  Other issues:   Long hierarchies become complex and difficult to manage. Long hierarchies lead to classes with complex interfaces.
    43. 43. 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. 
    44. 44. Polymorphism  When a single datatype exhibits different behaviors during execution.  Greek: Poly means “many”, morph means “form”. Polymorphism means “existing in many forms”.
    45. 45. Polymorphism  It is the other side of the same coin as Inheritance.  Polymorphism is the reason why we want to have inheritance.
    46. 46. 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. 
    47. 47. Polymorphism  Method Overriding when a subclass re-implements one or more methods from the superclass  changes the behavior of the method 
    48. 48. Polymorphism  You can pass a subclass to a method wherever a particular class is required. void add(Component c);  you can pass instance of Button, TextArea, Dialog, etc, since they all inherit the interface of Component
    49. 49. Polymorphism  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
    50. 50. T he End Java Fundamentals and Object-Oriented Programming The Complete Java Boot Camp

    ×