object-oriented programming (oop) lecture no. 3 downloaded from:

21
Object-Oriented Object-Oriented Programming (OOP) Programming (OOP) Lecture No. 3 Lecture No. 3 Downloaded From: www.bsit.zxq.net

Upload: ambrose-peters

Post on 21-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Object-Oriented Object-Oriented Programming (OOP)Programming (OOP)

Lecture No. 3Lecture No. 3

Downloaded From: www.bsit.zxq.net

Page 2: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Information HidingInformation Hiding

► Information is stored within the objectInformation is stored within the object

► It is hidden from the outside worldIt is hidden from the outside world

► It can only be manipulated by the It can only be manipulated by the object itselfobject itself

Downloaded From: www.bsit.zxq.net

Page 3: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – Information HidingExample – Information Hiding

►Ali’s name is stored within his brainAli’s name is stored within his brain

►We can’t access his name directlyWe can’t access his name directly

►Rather we can ask him to tell his nameRather we can ask him to tell his name

Downloaded From: www.bsit.zxq.net

Page 4: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – Information HidingExample – Information Hiding

►A phone stores several phone numbersA phone stores several phone numbers

►We can’t read the numbers directly We can’t read the numbers directly from the SIM cardfrom the SIM card

►Rather phone-set reads this Rather phone-set reads this information for usinformation for us

Downloaded From: www.bsit.zxq.net

Page 5: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Information HidingInformation HidingAdvantagesAdvantages

►Simplifies the model by hiding Simplifies the model by hiding implementation detailsimplementation details

► It is a barrier against change It is a barrier against change propagationpropagation

Downloaded From: www.bsit.zxq.net

Page 6: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

EncapsulationEncapsulation

►Data and behaviour are tightly coupled Data and behaviour are tightly coupled inside an objectinside an object

►Both the information structure and Both the information structure and implementation details of its implementation details of its operations are hidden from the outer operations are hidden from the outer worldworld

Downloaded From: www.bsit.zxq.net

Page 7: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – EncapsulationExample – Encapsulation

►Ali stores his personal information and Ali stores his personal information and knows how to translate it to the knows how to translate it to the desired languagedesired language

►We don’t knowWe don’t know How the data is storedHow the data is stored How Ali translates this informationHow Ali translates this information

Downloaded From: www.bsit.zxq.net

Page 8: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – EncapsulationExample – Encapsulation

►A Phone stores phone numbers in A Phone stores phone numbers in digital format and knows how to digital format and knows how to convert it into human-readable convert it into human-readable characterscharacters

►We don’t knowWe don’t know How the data is storedHow the data is stored How it is converted to human-readable How it is converted to human-readable

characterscharactersDownloaded From: www.bsit.zxq.net

Page 9: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Encapsulation – AdvantagesEncapsulation – Advantages

►Simplicity and claritySimplicity and clarity

►Low complexityLow complexity

►Better understandingBetter understanding

Downloaded From: www.bsit.zxq.net

Page 10: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Object has an InterfaceObject has an Interface

►An object encapsulates data and An object encapsulates data and behaviourbehaviour

►So how objects interact with each other?So how objects interact with each other?►Each object provides an interface Each object provides an interface

(operations)(operations)►Other objects communicate through this Other objects communicate through this

interfaceinterface

Downloaded From: www.bsit.zxq.net

Page 11: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – Interface of a CarExample – Interface of a Car

►Steer WheelsSteer Wheels►AccelerateAccelerate►Change GearChange Gear►Apply BrakesApply Brakes►Turn Lights On/OffTurn Lights On/Off

Downloaded From: www.bsit.zxq.net

Page 12: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – Interface of a Example – Interface of a PhonePhone

► Input NumberInput Number►Place CallPlace Call►Disconnect CallDisconnect Call►Add number to address bookAdd number to address book►Remove numberRemove number►Update numberUpdate number

Downloaded From: www.bsit.zxq.net

Page 13: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

ImplementationImplementation

►Provides services offered by the object Provides services offered by the object interfaceinterface

►This includesThis includes Data structures to hold object stateData structures to hold object state Functionality that provides required Functionality that provides required

servicesservices

Downloaded From: www.bsit.zxq.net

Page 14: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – Implementation of Example – Implementation of Gear BoxGear Box

►Data StructureData Structure Mechanical structure of gear boxMechanical structure of gear box

►FunctionalityFunctionality Mechanism to change gearMechanism to change gear

Downloaded From: www.bsit.zxq.net

Page 15: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – Implementation of Example – Implementation of Address Book in a PhoneAddress Book in a Phone

►Data StructureData Structure SIM cardSIM card

►FunctionalityFunctionality Read/write circuitryRead/write circuitry

Downloaded From: www.bsit.zxq.net

Page 16: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Separation of Interface & Separation of Interface & ImplementationImplementation

►Means change in implementation does Means change in implementation does not effect object interfacenot effect object interface

►This is achieved via principles of This is achieved via principles of information hiding and encapsulationinformation hiding and encapsulation

Downloaded From: www.bsit.zxq.net

Page 17: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – Separation of Example – Separation of Interface & ImplementationInterface & Implementation

►A driver can drive a car independent of A driver can drive a car independent of engine type (petrol, diesel)engine type (petrol, diesel)

►Because interface does not change Because interface does not change with the implementationwith the implementation

Downloaded From: www.bsit.zxq.net

Page 18: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Example – Separation of Example – Separation of Interface & ImplementationInterface & Implementation

►A driver can apply brakes independent A driver can apply brakes independent of brakes type (simple, disk)of brakes type (simple, disk)

►Again, reason is the same interfaceAgain, reason is the same interface

Downloaded From: www.bsit.zxq.net

Page 19: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Advantages of SeparationAdvantages of Separation

►Users need not to worry about a Users need not to worry about a change until the interface is samechange until the interface is same

►Low ComplexityLow Complexity

►Direct access to information structure Direct access to information structure of an object can produce errorsof an object can produce errors

Downloaded From: www.bsit.zxq.net

Page 20: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

MessagesMessages

►Objects communicate through Objects communicate through messagesmessages

►They send messages (stimuli) by They send messages (stimuli) by invoking appropriate operations on the invoking appropriate operations on the target objecttarget object

►The number and kind of messages that The number and kind of messages that can be sent to an object depends upon can be sent to an object depends upon its interfaceits interface

Downloaded From: www.bsit.zxq.net

Page 21: Object-Oriented Programming (OOP) Lecture No. 3 Downloaded From:

Examples – MessagesExamples – Messages

►A Person sends message (stimulus) A Person sends message (stimulus) “stop” to a Car by applying brakes“stop” to a Car by applying brakes

►A Person sends message “place call” A Person sends message “place call” to a Phone by pressing appropriate to a Phone by pressing appropriate buttonbutton

Downloaded From: www.bsit.zxq.net