Object-Oriented Programming
(OOP)
Lecture No. 2
Lecture Outlines
► 1. Information Hiding
► 2. Encapsulation
► 3. Interface
► 4. Implementation
► 5. Separation of Interface & Implementation
► 6. Messages
Information Hiding
► Information hiding is one of the most important principles
of OOP inspired from real life which says that all
information should not be accessible to all persons.
► Private information should only be accessible to its owner.
► In object oriented programming approach we have
objects with their attributes and behaviors that are hidden
from other classes, so we can say that object oriented
► programming follows the principle of information hiding.
Examples – Information Hiding
► Ali’s name and other personal information is stored in his
brain we can’t access this information directly. For getting
this information we need to ask Ali about it and it will be
up to Ali how much details he would like to share with us.
► An email server may have account information of millions
of people but it will share only our account information
with us if we request it to send anyone else accounts
information our request will be refused.
► 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
Encapsulation
► Encapsulation means “we have enclosed all
the characteristics of an object in the object
► itself”
► 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 – Encapsulation
► Alistores 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
Encapsulation – Advantages
► Simplicity and clarity
► Low complexity
► Better understanding
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
Example – Interface of a Phone
► Input Number
► Place Call
► Disconnect Call
► Add number to address book
► Remove number
► Update number
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
Example – Implementation of
Address Book in a Phone
► Data Structure
SIM card
► Functionality
Read/write circuitry
Separation of Interface &
Implementation
► Means, change in implementation does not
effect object interface
► Thisis 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
Example – Separation of
Interface & Implementation
►A driver can apply brakes independent of
brakes type (simple, disk)
► Again, reason is the same interface
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
Examples – Messages
►A Person sends message (stimulus) “stop”
to a Car by applying brakes
►A Person sends message “place call” to a
Phone by pressing appropriate button
Examples – Messages
► #include <iostream>
► #include <string>
► using namespace std;
► // Define a class called 'Car'
► class Car {
► public:
► // Constructor to initialize the object with attributes
► Car(string make, string model, int year)
► : make(make), model(model), year(year) {}
► // Method to display information about the car
► void displayInfo() {
► cout << year << " " << make << " " << model << endl;
► }
Examples – Messages
► // Get functions for the 'make' and 'model' attributes
► string getMake() const {
► return make;
► }
► string getModel() const {
► return model;
► }
► private:
► // Attributes of the Car class
► string make; // Make of the car
► string model; // Model of the car
► int year; // Manufacturing year of the car
► };
Examples – Messages
► int main() {
► // Create an instance (object) of the 'Car' class
► Car car1("Toyota", "Camry", 2022);
► // Access the 'make' and 'model' attributes using the getMake() and
getModel() methods
► cout << car1.getMake() << " " << car1.getModel() << endl; //
Output: Toyota Camry
► // Call the displayInfo() method
► car1.displayInfo(); // Output: 2022 Toyota Camry
► return 0;
► }