Docsity

Information Hiding - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Partial preview of the text

Download Information Hiding - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Information Hiding

  • Information is stored within the object
  • It is hidden from the outside world
  • It can only be manipulated by the object itself

Example – Information Hiding

  • A phone stores several phone numbers
  • We can’t read the numbers directly from the SIM card
  • Rather phone-set reads this information for us

Information Hiding

Advantages

  • Simplifies the model by hiding implementation details
  • It is a barrier against change propagation

Example – Encapsulation

  • Ali stores his personal information and knows how to translate it to the desired language
  • We don’t know
    • How the data is stored
    • How Ali translates this information

Example – Encapsulation

  • A Phone stores phone numbers in digital format and knows how to convert it into human-readable characters
  • We don’t know
    • How the data is stored
    • How it is converted to human-readable characters

Object has an Interface

  • An object encapsulates data and behaviour
  • So how objects interact with each other?
  • Each object provides an interface (operations)
  • Other objects communicate through this interface

Example – Interface of a Car

  • Steer Wheels
  • Accelerate
  • Change Gear
  • Apply Brakes
  • Turn Lights On/Off

Implementation

  • Provides services offered by the object interface
  • This includes
    • Data structures to hold object state
    • Functionality that provides required services

Example – Implementation of

Gear Box

  • Data Structure
    • Mechanical structure of gear box
  • Functionality
    • Mechanism to change gear

Separation of Interface &

Implementation

  • Means change in implementation does not effect object interface
  • This is achieved via principles of information hiding and encapsulation

Example – Separation of

Interface & Implementation

  • A driver can drive a car independent of engine type (petrol, diesel)
  • Because interface does not change with the implementation

Advantages of Separation

  • Users need not to worry about a change until the interface is same
  • Low Complexity
  • Direct access to information structure of an object can produce errors

Messages

  • Objects communicate through messages
  • They send messages (stimuli) by invoking appropriate operations on the target object
  • The number and kind of messages that can be sent to an object depends upon its interface