Information Hiding

Information is stored within the object. It is hidden from the outside world.

Example

  • 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

Encapsulation

Data and behavior are tightly coupled inside an object. Both the information structure and implementation details of its operations are hidden from the outer world.

Example

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

Advantages of Encapsulation

  • Clarity
  • Low complexity
  • Easy understanding

Object has an interface

  • An object encapsulates data and behavior
  • 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

Data Structure

  • Physical structure of gear box

Functionality

  • Mechanism to change gear
Print Friendly

Share this with your friends

Leave a Reply

Your email address will not be published. Required fields are marked *