Professional Documents
Culture Documents
Abstraction
Key Factors of OOP
The 3 key factors in OO programming
1. Inheritance
2. Encapsulation (data hiding)
3. Dynamic method binding
WHAT IS ENCAPSULATION?
Encapsulation is one of the fundamental concepts in object-
oriented programming (OOP).
In normal terms Encapsulation is defined as wrapping up of data
and information under a single unit.
In OOP, bundling or binding of data members and functions that
operate together inside a single class is called encapsulation.
Encapsulation ensures better control of your data, because you can
change one part of the code without affecting other parts.
Characteristics of Encapsulation:
Encapsulation keeps data and functions safe from outside interference and
misuse. Data encapsulation led to the important OOP concept of data hiding.
OOP supports the properties of encapsulation and data hiding through the
creation of user-defined types, called classes.
In encapsulation, all items defined in that specific class are kept private which
means they can be accessed only by that particular class, and not by any other
part of your program.
To achieve encapsulation, access specifiers(private, protected, public) plays
vital role.
How Encapsulation is achieved in a class?
To do this:
Make all the data members private.
Create public setter and getter functions for each data
member in such a way that:
the set function set the value of data member and get function
get the value of data member.
EXAMPLE: int main() {
public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};
UNDERSTANDING THE CONCEPT…!
• The salary attribute is private, which have restricted access.
• The public setSalary() method takes a parameter (s) and assigns it to the
salary attribute (salary = s).
• The public getSalary() method returns the value of the private salary
attribute.
• Inside main(), we create an object of the Employee class. Now we can use
the setSalary() method to set the value of the private attribute to 50000.
Then we call the getSalary() method on the object to return the value.
(GOT IT?)
ENCAPSULATION?
SUMMARY
The meaning of Encapsulation, is to make sure that "sensitive" data is
hidden from users.
To achieve this, you must declare class variables/attributes as private
(cannot be accessed from outside the class).
If you want others to read or modify the value of a private member,
you can provide public get and set methods.
WHAT IS ABSTRACTION?
Abstraction is one of the feature of OOP, where you show only
relevant details to the user and hide irrelevant details.
Which means displaying only essential information and hiding the
details.
For example, when you send an email to someone you just click send
and you get the message status only (sent, not sent, or draft etc),
what actually happens when you click send, how data is transmitted
over network to the recipient is hidden from you (because it is
irrelevant to you).
Types of Abstraction:
In the C++ language, there are two types of abstraction:
#include <iostream>
#include <math.h>