The packing of data and functions into a single unit in a program is known as ?

a) polymorphism

(b) abstraction

(c) encapsulation

(d) inheritance 

Ans: (c) encapsulation

Encapsulation is one among the elemental concepts in object-oriented programming (OOP). It describes the thought of bundling data and methods that employment thereon data within one unit, e.g., a category in Java.

This concept is additionally often wont to hide the interior representation, or state, of an object from the surface . this is often called information hiding. the overall idea of this mechanism is straightforward . If you've got an attribute that's not visible from the surface of an object, and bundle it with methods that provide read or write access thereto , then you'll hide specific information and control access to the interior state of the thing .

If you’re conversant in any object-oriented programing language , you almost certainly know that these methods as getter and setter methods. because the names indicate, a getter method retrieves an attribute, and a setter method changes it. counting on the methods that you simply implement, you'll decide if an attribute are often read and altered , or if it’s read-only, or if it's not visible in the least . As i will be able to show you later, you'll also use the setter method to implement additional validation rules to make sure that your object always features a valid state.

Encapsulation in C++


In normal terms Encapsulation is defined as wrapping from data and knowledge under one unit. In Object Oriented Programming, Encapsulation is defined as binding together the info and therefore the functions that manipulates them.

Consider a true life example of encapsulation, during a company there are different sections just like the accounts section, finance section, sales section etc. The finance section handles all the financial transactions and keep records of all the info associated with finance. Similarly the sales section handles all the sales related activities and keep records of all the sales. Now there may arise a situation when for a few reason a politician from finance section needs all the info about sales during a particular month. during this case, he's not allowed to directly access the info of sales section. He will first need to contact another officer within the sales section then request him to offer the actual data. this is often what encapsulation is. Here the info of sales section and therefore the employees which will manipulate them are wrapped under one name “sales section”.

Encapsulation also cause data abstraction or hiding. As using encapsulation also hides the info . within the above example the info of any of the section like sales, finance or accounts is hidden from the other section.

In C++ encapsulation are often implemented using Class and access modifiers. check out the below program:

// c++ program to elucidate 
// Encapsulation 

#include 

using namespace std; 

class Encapsulation 

 private: 

 // data hidden from outside world 

 int x; 

 public: 

 // function to line value of 

 // variable x 

 void set(int a) 

 { 

 x =a; 

 } 

 // function to return value of 

 // variable x 

 int get() 

 { 

 return x; 

 } 
}; 

// main function 

int main() 

 Encapsulation obj; 

 obj.set(5); 

Cout<<obj.get( );

return 0 ;

}

Output:

5

In the above program the variable x is formed private. This variable are often accessed and manipulated only using the functions get() and set() which are present inside the category . Thus we will say that here, the variable x and therefore the functions get() and set() are binded together which is nothing but encapsulation.

Encapsulation in Java


Encapsulation is defined because the wrapping from data under one unit. it's the mechanism that binds together code and therefore the data it manipulates.Other thanks to believe encapsulation is, it's a protective shield that forestalls the info from being accessed by the code outside this shield.

Technically in encapsulation, the variables or data of a category is hidden from the other class and may be accessed only through any member function of own class during which they're declared.

As in encapsulation, the info during a class is hidden from other classes using the info hiding concept which is achieved by making the members or methods of sophistication as private and therefore the class is exposed to the top user or the planet without providing any details behind implementation using the abstraction concept, so it's also referred to as combination of data-hiding and abstraction..

Encapsulation are often achieved by: Declaring all the variables within the class as private and writing public methods within the class to line and obtain the values of variables.

Encapsulation in Python


Encapsulation is one among the elemental concepts in object-oriented programming (OOP). It describes the thought of wrapping data and therefore the methods that employment on data within one unit. This puts restrictions on accessing variables and methods directly and may prevent the accidental modification of knowledge . to stop accidental change, an object’s variable can only be changed by an object’s method. Those sort of variables are referred to as private varibale.

A class is an example of encapsulation because it encapsulates all the info that's member functions, variables, etc.

Consider a real-life example of encapsulation, during a company, there are different sections just like the accounts section, finance section, sales section etc. The finance section handles all the financial transactions and keeps records of all the info associated with finance. Similarly, the sales section handles all the sales-related activities and keeps records of all the sales. Now there may arise a situation when for a few reason a politician from the finance section needs all the info about sales during a particular month. during this case, he's not allowed to directly access the info of the sales section. He will first need to contact another officer within the sales section then request him to offer the actual data. this is often what encapsulation is. Here the info of the sales section and therefore the employees which will manipulate them are wrapped under one name “sales section”. As using encapsulation also hides the info . during this example, the info of any of the sections like sales, finance or accounts are hidden from the other section.

Post a Comment

0 Comments