0% found this document useful (0 votes)
14 views

Session 8

Abstraction is the process of hiding internal implementation details and showing only essential functions and properties. Encapsulation binds together code and data by wrapping related properties and methods within a class. Access specifiers like public and private enforce visibility restrictions and protect data from misuse. Together, abstraction and encapsulation improve modularity, flexibility and security of object-oriented programs.

Uploaded by

yadvji568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Session 8

Abstraction is the process of hiding internal implementation details and showing only essential functions and properties. Encapsulation binds together code and data by wrapping related properties and methods within a class. Access specifiers like public and private enforce visibility restrictions and protect data from misuse. Together, abstraction and encapsulation improve modularity, flexibility and security of object-oriented programs.

Uploaded by

yadvji568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

18CSC202J - OBJECT ORIENTED

DESIGN AND PROGRAMMING

Session 8

Topic : Feature Abstraction and


Encapsulation, Application of Abstraction and
Encapsulation
Abstraction
• Abstraction is the process of providing only the essential details to the outside world
and hiding the internal details
• That is, representing only the essential details and hiding the implementation details

• Separation of the interface and implementation details of the program

• Real life examples: AC, Car,..


Abstraction
• While classifying a class, both data members and member
functions are expressed in the code. But, while using an
object (that is an instance of a class) the built-in data types
and the members in the class get ignored which is known as
data abstraction.

• C++ provides a great level of data abstraction. In C++, we use


classes to define our own abstract data types (ADT).
Programmers can use the "cout" object of class ostream for
data streaming to standard output like this:
Abstraction using Classes and Access
Specifiers
• Class helps us to group data members and member functions

• Access specifiers enforce restrictions on class members


• We can decide which data member will be visible to the outside world
and which is not
• Members declared as public can be accessed from anywhere in the
program

• Members declared as private can be accessed only from within the


class. They are not allowed to be accessed from any part of code
outside the class
Here is an example of declaring Public members of C++ class
#include <iostream>
using namespace std;
class sample {
public:
int gl, g2;
public:
void val()
{ cout << "Enter Two values : "; cin >> gl >> g2;
}
void display()
{
cout << gl << " " << g2;
cout << endl;
} };
int main() {
sample s;
s.val();
s.display();
}
Here is a Private member example in which member data cannot be accessed
• :

#include <iostream> outside the class


using namespace std; If you execute the above program, the private
class sample {
public:
member function will not be accessible and
int gl, g2; hence the following error message will appear
public: like this in some compiler:
void val()
{ cout << "Enter Two values : "; cin >> gl >> g2;
}
private:
void display()
{ cout << gl << " " << g2;
cout << endl;
}
};
int main()
{
sample s;
s.val();
s.display();
}
OUTPUT

Please enter the two numbers :5 5


The Sum of two number is : 10
Advantages of Abstraction
• Implementation details of the class are protected from the inadvertent user
level errors
• A programmer does not need to write the low level code
• Data Abstraction avoids code duplication

• We can reuse code

• Internal implementation can be changed without affecting the user level code
class implementAbstraction
int main()
{
private: {
int a, b; implementAbstraction obj;
obj.set(10, 20);
public: obj.display();
return 0;
// method to set values of
}
// private members
void set(int x, int y)
{
a = x;
Output:
b = y;
} a = 10 b = 20

void display()
{
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
Application of Abstraction
• For example, when you send an email to someone you just click send and
you get the success message, what actually happens when you click send,
how data is transmitted over network to the recipient is hidden from you
ENCAPSULATION
• DATA ENCAPSULATION
– Encapsulation is defined as wrapping up of data and information under a
single unit.
– In OOP, Encapsulation is defined as binding together the data and the
functions that manipulates them.
– Combining data and functions into a single unit called class and the process is
known as Encapsulation.

• Provides security, flexibility and easy maintainability


Encapsulation helps us in binding the data(instance variables) and the member
functions(that work on the instance variables) of a class.

Encapsulation is also useful in hiding the data(instance variables) of a class from


an illegal direct access.

Encapsulation also helps us to make a flexible code which is easy to change and
maintain.
Example
• The common example of encapsulation is Capsule.

• In capsule all medicine are encapsulated in side capsule.

• Real-life Example:

– In an organization, there are different sections like the Finance Section, Sales section, etc…

– If an official from Finance section needs all the data about sales in a particular month, he
is not allowed to directly access the data of sales section. He has to contact some other
officer in Sales section
ENCAPSULATION
ADVANTAGES
• Encapsulated classes reduce complexity.

• Help protect our data. A client cannot change an Account's balance if we


encapsulate it.
• Encapsulated classes are easier to change.
Nesting of Member Functions
Private Member Functions

Note: s.read() will


not work

Note: A private member function cannot be accessed outside the class. So it cannot be
accessed inside the main function
MCQ
1. Wrapping data and its related functionality into a single entity is known as
_____________
a) Abstraction
b) Encapsulation
c) Polymorphism
d) Modularity

2. Which concept enables programmers to assign a different meaning or usage


to a variable, function, or an object in different contexts?
(a) Inheritance
(b) Message passing
(c) Polymorphism
(d) Abstraction
Examples
3. Which of the following is the functionality of ‘Data Abstraction’?
a) Reduce Complexity
b) Binds together code and data
c) Parallelism
d) None of the mentioned

4. How will a class protect the code inside it?


a) Using Access specifiers
b) Abstraction
c) Use of Inheritance
d) All of the mentioned
Examples
5. What is the output of this program?
class Test {
int a;
public int b;
private int c;
}
class AcessTest {
public static void main(String args[])
{
Test ob = new Test();
ob.a = 10;
ob.b = 20;
ob.c = 30;
System.out.println(" Output :a, b, and c" + ob.a + " " + ob.b + " " + ob.c);
}
}
a) Compilation error
b) Run time error Private members of a class cannot be accessed
c) Output : a, b and c 10 20 30 directly. In the above program, the variable c is
d) None of the mentioned a private member of class ‘Test’ and can only
be accessed through its methods.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy