INTRODUCTION TO OOP
Objective:
•Know the difference between functional programm
and OOP
• Know basic terminology in OOP
• Know the importance of OOP
• Know four design principles of OOP
• Know OOP programming languages
1
STRUCTURED vs. OO PROGRAMMING
STRUCTURED PROGRAMMING:
MAIN PROGRAM GLOBAL DATA
FUNCTION FUNCTION 2 FUNCTION 3
1
FUNCTION 4 FUNCTION 5
2
Structured Programming
Using function
Function & program is divided into modules
Every module has its own data and function which
can be called by other modules.
3
OBJECT ORIENTED PROGRAMMING
Object 2
Object 1
Data Data
Function Function
Object 3
Data
Function
4
OBJECT ORIENTED PROGRAMMING
OBJECTS HAVE BOTH DATA AND METHODS
OBJECTS OF THE SAME CLASS HAVE THE SAME
DATA ELEMENTS AND METHODS
OBJECTS SEND AND RECEIVE MESSAGES TO INVOKE
ACTIONS
KEY IDEA IN OBJECT-ORIENTED:
THE REAL WORLD CAN BE ACCURATELY DESCRIBED AS A
COLLECTION OF OBJECTS THAT INTERACT.
5
BASIC TERMINOLOGY
OBJECT
- USUALLY A PERSON, PLACE OR THING (A NOUN)
METHOD
- AN ACTION PERFORMED BY AN OBJECT (A VERB)
ATTRIBUTE
- DESCRIPTION OF OBJECTS IN A CLASS
CLASS
- A CATEGORY OF SIMILAR OBJECTS (SUCH AS
AUTOMOBILES)
- DOES NOT HOLD ANY VALUES OF THE OBJECT’S
ATTRIBUTES
6
Example for attributes and methods
Attributes: Methods:
◦ manufacturer’s name ◦ Define data items (specify
◦ model name manufacturer’s name, model, year,
◦ year made etc.)
◦ color ◦ Change a data item (color, engine,
etc.)
◦ number of doors
◦ Display data items
◦ size of engine
◦ Calculate cost
◦ etc.
◦ etc.
7
Why OOP?
SAVE DEVELOPMENT TIME (AND COST) BY
REUSING CODE
once an object class is created it can be used in other
applications
EASIER DEBUGGING
classes can be tested independently
reused objects have already been tested
8
Design Principles of OOP
FOUR MAIN DESIGN PRINCIPLES OF OBJECT-
ORIENTED PROGRAMMING(OOP):
ENCAPSULATION
ABSTRACTION
POLYMORPHISM
INHERITANCE
9
Encapsulation
ALSO KNOWN AS DATA HIDING
ONLY OBJECT’S METHODS CAN MODIFY
INFORMATION IN THE OBJECT.
ANALOGY:
ATM MACHINE CAN ONLY UPDATE
ACCOUNTS OF ONE PERSON OR OBJECT
ONLY.
10
Abstraction
Focus only on the important facts about the problem at
hand
to design, produce, and describe so that it can be easily
used without knowing the details of how it works.
Analogy:
When you drive a car, you don’t have to know how the
gasoline and air are mixed and ignited.
Instead you only have to know how to use the controls.
Draw map
11
Polymorphism
THE SAME WORD OR PHRASE CAN MEAN
DIFFERENT THINGS IN DIFFERENT CONTEXTS
ANALOGY:
IN ENGLISH, BANK CAN MEAN SIDE OF A
RIVER OR A PLACE TO PUT MONEY
MOVE -
12
Function Overloading
The operation of one function depends on the argument passed to it.
Example: Fly(), Fly(low), Fly(150)
13
Inheritance
INHERITANCE—A WAY OF ORGANIZING
CLASSES
TERM COMES FROM INHERITANCE OF TRAITS
LIKE EYE COLOR, HAIR COLOR, AND SO ON.
CLASSES WITH PROPERTIES IN COMMON CAN
BE GROUPED SO THAT THEIR COMMON
PROPERTIES ARE ONLY DEFINED ONCE.
SUPERCLASS – INHERIT ITS ATTRIBUTES &
METHODS TO THE SUBCLASS(ES).
SUBCLASS – CAN INHERIT ALL ITS
SUPERCLASS ATTRIBUTES & METHODS BESIDES
HAVING ITS OWN UNIQUE ATTRIBUTES &
METHODS.
14
An Inheritance Hierarchy
Superclass
Vehicle
Subclasses
Automobile Motorcycle Bus
Sedan Sports Car Luxury Bus School Bus
What properties does each vehicle inherit from the types
of vehicles above it in the diagram?
15
Object-Oriented Programming Languages
PURE OO LANGUAGES
SMALLTALK, EIFFEL, ACTOR, JAVA
HYBRID OO LANGUAGES
C++, OBJECTIVE-C, OBJECT-PASCAL
16