object oriented programming principles lecturer: kalamullah ramli electrical engineering dept....
TRANSCRIPT
Object Oriented Programming PrinciplesObject Oriented Programming Principles
Lecturer: Lecturer: Kalamullah RamliKalamullah Ramli
Electrical Engineering Dept.Electrical Engineering Dept.University of IndonesiaUniversity of Indonesia
Session-3Session-3
Slide - Slide - 22OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Object-Oriented Object-Oriented Programming:Programming:A New Programming A New Programming Paradigm… Paradigm… What is Matlab's programming What is Matlab's programming
paradigm?paradigm? Matlab is Matlab is proceduralprocedural: the focus is on : the focus is on
the the functionsfunctions (procedures) (procedures)
What is the programming paradigm What is the programming paradigm in OOP?in OOP?
As the name suggests, in OOP the As the name suggests, in OOP the focus is onfocus is on
OBJECTSOBJECTS
This doesn't say much unless we This doesn't say much unless we understand what we mean with the understand what we mean with the term term ObjectObject… So let's move on.… So let's move on.
Slide - Slide - 33OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Objects Combine Properties & Objects Combine Properties & Behavior Behavior [1/2][1/2]
Remember following Structures: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 A structure contains data in an organized fashion.fashion.
Slide - Slide - 44OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Objects Combine Properties & Objects Combine Properties & Behavior Behavior [2/2][2/2]
If we add functions or If we add functions or methodsmethods to a to a structure, it becomes an object:structure, it becomes an object:
person.print_address(); person.set_lastName('Paredis');
Slide - Slide - 55OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
More Examples of Objects More Examples of Objects [1/2][1/2]
Car:Car: Properties: color, speed, fuel consumption, HP… Behaviors: start, stop, turning, …
Triangle:Triangle: Properties: area, perimeter, linestyle, location,… Behaviors: translate, rotate, shrink, flip,…
Date:Date: Properties: year, month, day, … Behaviors: setDate, getMonth, isGreater, …
Slide - Slide - 66OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
More Examples of Objects More Examples of Objects [1/2][1/2]
Properties: Properties: information about the objectinformation about the object Behaviors: Behaviors: methods to set, get and methods to set, get and
process propertiesprocess properties
Combining properties and behaviors in Combining properties and behaviors in objects is calledobjects is called
EncapsulationEncapsulation
Slide - Slide - 77OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Objects Combine Properties and Objects Combine Properties and Behavior:Behavior:So What? Why should we care? So What? Why should we care? [1/2][1/2]
Black Box PhilosophyBlack Box Philosophy:: Objects perform Objects perform
computation by making computation by making requests of each other requests of each other through the passing of through the passing of messagesmessages
The only way to interact The only way to interact with an object is through with an object is through its methods!its methods!
http://java.sun.com/docs/books/tutorial/java/concepts/object.htmlhttp://catalog.com/softinfo/objects.html
Messages
Code
Data
Slide - Slide - 88OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Objects Combine Properties and Objects Combine Properties and Behavior:Behavior:So What? Why should we care? So What? Why should we care? [2/2][2/2]
This is called This is called Information HidingInformation Hiding (the data is hidden from the (the data is hidden from the
user)user)
The collection of all methods is The collection of all methods is called thecalled the interface interface of the objectof the object
http://java.sun.com/docs/books/tutorial/java/concepts/object.htmlhttp://catalog.com/softinfo/objects.html
Slide - Slide - 99OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Encapsulation and Data Hiding Encapsulation and Data Hiding [1/2][1/2]
Properties Properties (Variables)(Variables) Speed RPM Current Gear
Methods (Functions)Methods (Functions) Braking Acceleration Turn Changing Gears
Slide - Slide - 1010OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Encapsulation and Data Hiding Encapsulation and Data Hiding [2/2][2/2]
These methods are independent of These methods are independent of how the bicycle has been built how the bicycle has been built (hides the implementation)(hides the implementation)
You can control access to members You can control access to members of an objectof an object
Slide - Slide - 1111OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Working with Objects: Working with Objects: Messages Messages [1/2][1/2]
Objects perform computation by Objects perform computation by making requests of each other through making requests of each other through the passing of messages the passing of messages
Parts of a message –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\
Slide - Slide - 1212OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Working with Objects: Working with Objects: Messages Messages [2/2][2/2]
Different objects may respond differently Different objects may respond differently to an identical message:to an identical message: bicycle.changeGears(lowerGear) car.changeGears(lowerGear)
The same name and the same argument, The same name and the same argument, but a different method = but a different method = POLYMORPHISMPOLYMORPHISM
A method is defined only in the scope of A method is defined only in the scope of a particular type of object, called classa particular type of object, called class
Polymorphism is also called: Polymorphism is also called: function function overloadingoverloading
Slide - Slide - 1313OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Class and Object Class and Object [1/2][1/2]
Now that we know what an object is all Now that we know what an object is all about, let's look at how we can about, let's look at how we can organizeorganize these objects these objects
Class =Class = A blueprint, or prototype, that A blueprint, or prototype, that defines the variables and the methods defines the variables and the methods common to all objects of a certain kindcommon to all objects of a certain kind
Objects are individual instances of a class
Slide - Slide - 1414OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Class and Object Class and Object [2/2][2/2]
House Plans:House Plans:
the architectural drawings the architectural drawings that describe how a house that describe how a house is to be constructedis to be constructed
A House:A House:
The house built from the The house built from the plans is an instance of the plans is an instance of the House Class. The process House Class. The process of building the house is of building the house is called called InstantiationInstantiation
Slide - Slide - 1515OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Organizing Objects: Inheritance Organizing Objects: Inheritance [1/2][1/2]
Super class(parent)
Sub class(child)
Children inherit• properties• behaviors
Slide - Slide - 1616OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Organizing Objects: Inheritance Organizing Objects: Inheritance [2/2][2/2]
Sub classSub class (Child) (Child)• A class that is derived from a particular
class, perhaps with one or more classes in between
Super classSuper class (Parent) (Parent)• A class from which a particular class is
derived, perhaps with one or more classes in between
Inheritance promotesInheritance promotes Reuse of code Better Management of code
Slide - Slide - 1717OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Abstraction in OOP Abstraction in OOP [1/2][1/2]
Abstractions Abstractions reveal causes and effects, reveal causes and effects, expose patterns and frameworks and expose patterns and frameworks and separate what's important from what's notseparate what's important from what's not
Abstraction in programmingAbstraction in programming helps you helps you make your ideas concrete in your code make your ideas concrete in your code without obscuring the architecture with without obscuring the architecture with detailsdetails
In procedural languages:In procedural languages: structures and functions are abstractions
Slide - Slide - 1818OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Abstraction in OOP Abstraction in OOP [2/2][2/2]
OOP takes abstraction one step further OOP takes abstraction one step further through:through:
encapsulationencapsulation data hidingdata hiding polymorphismpolymorphism InheritanceInheritance
Slide - Slide - 1919OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Object-Oriented Programming Object-Oriented Programming [1/2][1/2]
A Quick Review:A Quick Review:
““OOP is more a way of thinking than just a OOP is more a way of thinking than just a
programming technique” – “programming technique” – “conceptual tool that you conceptual tool that you
use in thinking how to solve a problem”use in thinking how to solve a problem”
Computer Computer objectsobjects form the basis form the basis
Objects treated as real life objectsObjects treated as real life objects Identities Properties Behaviors
Data and functionality Data and functionality encapsulatedencapsulated in an object in an object
Data Data hiddenhidden behind methods behind methods
Slide - Slide - 2020OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Object-Oriented Programming Object-Oriented Programming [2/2][2/2]
Objects organized in class hierarchies: inheritanceObjects organized in class hierarchies: inheritance
Objects interact through messages – methodsObjects interact through messages – methods
Polymorphism: the same message has a different Polymorphism: the same message has a different
meaning for different objectsmeaning for different objects
Slide - Slide - 2121OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Benefits of Object Oriented Benefits of Object Oriented ProgrammingProgramming
Analysis and Design made easierAnalysis and Design made easier
Understanding of code made easierUnderstanding of code made easier
Code ReuseCode Reuse
Ease of maintenance and enhancement Ease of maintenance and enhancement
Simplifies collaborationSimplifies collaboration
Fewer and shorter design iterationsFewer and shorter design iterations
Slide - Slide - 2222OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Examples of Programming Examples of Programming LanguagesLanguages
Procedural languagesProcedural languages C FORTRAN BASIC Pascal
Object Oriented languagesObject Oriented languages C++ Smalltalk Java
Slide - Slide - 2323OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Assignment#2a: OOP vs Procedural Assignment#2a: OOP vs Procedural ProgrammingProgramming
Object-OrientedObject-Oriented
ProceduralProcedural
Due date: Due date: Friday, February 27Friday, February 27thth, 2004, 2004
Slide - Slide - 2424OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Assignment#2b: Object Oriented Assignment#2b: Object Oriented PhilosophyPhilosophy
““Simulate the functionality of your computer Simulate the functionality of your computer using the object oriented using the object oriented philosophyphilosophy” ”
Draw a diagram of the components present in Draw a diagram of the components present in the computer. What is the flow of information the computer. What is the flow of information between these components? Leverage from between these components? Leverage from previous lectures.previous lectures.
Identify the classes and objects that you Identify the classes and objects that you would need to simulate these components. would need to simulate these components. How do these classes interact?How do these classes interact?
Identify the variables and methods that you Identify the variables and methods that you would assign to each class.would assign to each class.
Due Date: Due Date: Friday, March 5Friday, March 5thth, 2004, 2004
Slide - Slide - 2525OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
References and Additional References and Additional ReadingReading
““Object Oriented Programming, ” Object Oriented Programming, ” http://www.toodarkpark.net/computers/objhttp://www.toodarkpark.net/computers/objc/oop.htmlc/oop.html
““Core Java, ” Cay S. Horstmann and Gary Core Java, ” Cay S. Horstmann and Gary Cornell, Cornell, Sun MicrosystemsSun Microsystems
““ Thinking in C++Thinking in C++, , ”” Bruce Eckel, Bruce Eckel, http://www.mindview.net/Books/TICPP/Thinhttp://www.mindview.net/Books/TICPP/ThinkingInCPP2e.htmlkingInCPP2e.html
“ “ Introduction to Object Oriented Introduction to Object Oriented Programming, ” Timothy Budd, Programming, ” Timothy Budd, ftp://ftp.cs.orst.edu/pub/budd/oopintro/3rdftp://ftp.cs.orst.edu/pub/budd/oopintro/3rdEdition/info.htmlEdition/info.html
Slide - Slide - 2626OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
ClosureClosure
LearningLearning Object oriented programming is more a philosophy than Object oriented programming is more a philosophy than
mere technique to develop programs…mere technique to develop programs… This philosophy provides modularity to programsThis philosophy provides modularity to programs Modularity provides openness to change…Modularity provides openness to change… Enables developing complex systemsEnables developing complex systems
Food for ThoughtFood for Thought How will object oriented philosophy help in the future How will object oriented philosophy help in the future
of software development?of software development? How can the object oriented philosophy based on How can the object oriented philosophy based on
modularity be used in your field of specialization?modularity be used in your field of specialization? What changes are required to adapt this philosophy to What changes are required to adapt this philosophy to
your field?your field?
Slide - Slide - 2727OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
The EndThe End
QUESTIONS & QUESTIONS & COMMENTS ?COMMENTS ?