1. Lecturer:
Session-3 Kalamullah Ramli
Electrical Engineering Dept.
University of Indonesia
Object Oriented Programming Principles
2. Object-Oriented Programming:
A New Programming Paradigm…
What is Matlab's programming paradigm?
Matlab is procedural: the focus is on the
functions (procedures)
What is the programming paradigm in OOP?
As the name suggests, in OOP the focus is on
OBJECTS
This doesn't say much unless we understand
what we mean with the term Object… So
let's move on.
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 2
3. Objects Combine Properties & Behavior [1/2]
Remember following Structures:
person.firstname = 'John';
person.lastname = 'Leonard';
person.address1 = '803 Shallowford
Lane';
person.city = 'Peachtree City';
person.state = 'GA';
person.zip = '30269-4289';
A structure contains data in an organized fashion.
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 3
4. Objects Combine Properties & Behavior [2/2]
If we add functions or methods to a structure, it
becomes an object:
person.print_address();
person.set_lastName('Paredis');
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 4
6. More Examples of Objects [1/2]
Properties: information about the object
Behaviors: methods to set, get and process
properties
Combining properties and behaviors in objects is
called
Encapsulation
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 6
7. Objects Combine Properties and Behavior:
So What? Why should we care? [1/2]
Black Box Philosophy:
Objects perform computation Messages
by making requests of each
other through the passing of
messages
Code
The only way to interact with
an object is through its Data
methods!
http://java.sun.com/docs/books/tutorial/java/concepts/object.html
OOP Lecture 2004 http://catalog.com/softinfo/objects.html
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 7
8. Objects Combine Properties and Behavior:
So What? Why should we care? [2/2]
This is called Information Hiding
(the data is hidden from the user)
The collection of all methods is called
the interface of the object
http://java.sun.com/docs/books/tutorial/java/concepts/object.html
OOP Lecture 2004 http://catalog.com/softinfo/objects.html
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 8
9. Encapsulation and Data Hiding [1/2]
Properties (Variables)
Speed
RPM
Current Gear
Methods (Functions)
Braking
Acceleration
Turn
Changing Gears
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 9
10. Encapsulation and Data Hiding [2/2]
These methods are independent of how the
bicycle has been built (hides the
implementation)
You can control access to members of an
object
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 10
11. Working with Objects: Messages [1/2]
Objects perform computation by making
requests of each other through the passing of
messages
Parts of a message –
The object to which the message is addressed
The name of the method to perform
Any parameters needed by the method
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 11
12. Working with Objects: Messages [2/2]
Different objects may respond differently to an
identical message:
bicycle.changeGears(lowerGear)
car.changeGears(lowerGear)
The same name and the same argument, but a
different method = POLYMORPHISM
A method is defined only in the scope of a
particular type of object, called class
Polymorphism is also called: function overloading
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 12
13. Class and Object [1/2]
Now that we know what an object is all about,
let's look at how we can organize these objects
Class = A blueprint, or prototype, that defines
the variables and the methods common to all
objects of a certain kind
Objects are individual
instances of a class
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 13
14. Class and Object [2/2]
House Plans: A House:
the architectural drawings that The house built from the plans
describe how a house is to be is an instance of the House Class.
constructed The process of building the
house is called Instantiation
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 14
15. Organizing Objects: Inheritance [1/2]
Children inherit Super class
• properties (parent)
• behaviors
Sub class
(child)
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 15
16. Organizing Objects: Inheritance [2/2]
Sub class (Child)
• A class that is derived from a particular class,
perhaps with one or more classes in between
Super class (Parent)
• A class from which a particular class is derived,
perhaps with one or more classes in between
Inheritance promotes
Reuse of code
Better Management of code
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 16
17. Abstraction in OOP [1/2]
Abstractions reveal causes and effects, expose
patterns and frameworks and separate what's
important from what's not
Abstraction in programming helps you make your
ideas concrete in your code without obscuring the
architecture with details
In procedural languages:
structures and functions are abstractions
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 17
18. Abstraction in OOP [2/2]
OOP takes abstraction one step further through:
encapsulation
data hiding
polymorphism
Inheritance
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 18
19. Object-Oriented Programming [1/2]
A Quick Review:
“OOP is more a way of thinking than just a programming
technique” – “conceptual tool that you use in thinking how to
solve a problem”
Computer objects form the basis
Objects treated as real life objects
Identities
Properties
Behaviors
Data and functionality encapsulated in an object
Data hidden behind methods
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 19
20. Object-Oriented Programming [2/2]
Objects organized in class hierarchies: inheritance
Objects interact through messages – methods
Polymorphism: the same message has a different meaning for
different objects
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 20
21. Benefits of Object Oriented
Programming
Analysis and Design made easier
Understanding of code made easier
Code Reuse
Ease of maintenance and enhancement
Simplifies collaboration
Fewer and shorter design iterations
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 21
22. Examples of Programming Languages
Procedural languages
C
FORTRAN
BASIC
Pascal
Object Oriented languages
C++
Smalltalk
Java
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 22
23. Assignment#2a: OOP vs Procedural Programming
Object-Oriented Procedural
Due date: Friday, February 27th, 2004
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 23
24. Assignment#2b: Object Oriented Philosophy
“Simulate the functionality of your computer using the
object oriented philosophy”
Draw a diagram of the components present in the
computer. What is the flow of information between
these components? Leverage from previous lectures.
Identify the classes and objects that you would need to
simulate these components. How do these classes
interact?
Identify the variables and methods that you would
assign to each class.
Due Date: Friday, March 5th, 2004
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 24
25. References and Additional Reading
“Object Oriented Programming, ”
http://www.toodarkpark.net/computers/objc/oop.h
tml
“Core Java, ” Cay S. Horstmann and Gary Cornell,
Sun Microsystems
“ Thinking in C++, ” Bruce Eckel,
http://www.mindview.net/Books/TICPP/ThinkingInC
PP2e.html
“ Introduction to Object Oriented Programming, ”
Timothy Budd,
ftp://ftp.cs.orst.edu/pub/budd/oopintro/3rdEdition
/info.html
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 25
26. Closure
Learning
Object oriented programming is more a philosophy than mere
technique to develop programs…
This philosophy provides modularity to programs
Modularity provides openness to change…
Enables developing complex systems
Food for Thought
How will object oriented philosophy help in the future of software
development?
How can the object oriented philosophy based on modularity be
used in your field of specialization?
What changes are required to adapt this philosophy to your field?
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 26
27. The End
QUESTIONS & COMMENTS ?
OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Ing. Ramli, Slide - 27