Notes from my guru Sandeep Karan sir.
Encapsulation [A information(data or implementation) hiding mechanism]
capsule means in plain english
1. A structure that encloses a body part (function or class)
2. A small container
Encapsulation is a process of binding data members (variables, properties) and member functions (methods, operation) into a single unit”. eg. Function And Class is the best example of encapsulation.
capsule means in plain english
1. A structure that encloses a body part (function or class)
2. A small container
Encapsulation is a process of binding data members (variables, properties) and member functions (methods, operation) into a single unit”. eg. Function And Class is the best example of encapsulation.
Encapsulation is a process of hiding all the internal details of an object(real entity, not class ka object) from the outside world.
The ultimate goal of Encapsulation is to achieve SIMPLICITY in our code.
__________________________________________________________________________
__________________________________________________________________________
In “Students” Example let's Implement Encapsulation.
class Students //No matter there is union or struct
{
public double totalMarks; //data, variable or attribute
public double MarksScored;
class Students //No matter there is union or struct
{
public double totalMarks; //data, variable or attribute
public double MarksScored;
public double GetPercentageMarks() //operation
{
double Percentage =(MarksScored /totalMarks) * 100;
return Percentage;
}
}
{
double Percentage =(MarksScored /totalMarks) * 100;
return Percentage;
}
}
Students ComputerStd = new Students();
In above code we have encapsulated totalMarks, MarksScored and method GetPercentageMarks. While creating a “ComputerStd” object, the implementation of GetPercentageMarks will not be shown.
In above code we have encapsulated totalMarks, MarksScored and method GetPercentageMarks. While creating a “ComputerStd” object, the implementation of GetPercentageMarks will not be shown.
________________________________________________________________________
Encapsulation help us to achieve a rule of software enginerring called simplicity (and by simplicity we achieve maintainability & security)
Encapsulation help us to achieve a rule of software enginerring called simplicity (and by simplicity we achieve maintainability & security)
- encapsulation is achieved by class, function, delegate, property.
________________________________________________________________________
Function is also example of Encapsulation
void add()
{
int a = 4;
int b = 5; //data, variable or attribute
int c;
c = a + b; //operation
cout << "Addtion of 2 no is: " << c;
}
________________________________________________________________________
Advance definition:
Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
OR
Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation (contractual--Relating to or part of a binding legal agreement)
==========================================================
Through encapsulation a class can hide the internal details of how an object does something. Encapsulation solves the problem at the implementation level.
A class or structure can specify how accessible each of its members (variables, properties, and methods) is to code outside of the class or structure. Encapsulation simplifies the interaction between objects. An object can use another object without knowing all its data or how its data is maintained. For example, a Client object might have name, address, company, and department properties. If a Bank object wants to use a Client object, it can request the name and address for the bank without needing to know the company and department details of the Client object.
With the help of encapsulation, a class can change the internal implementation without hurting the overall functionality of the system. Encapsulation protects abstraction.
_________________________________________________________________________
class Counter
{
private int Count;
public Counter()
{
Count = 0;
}
public void setCount( int newVal )
{
Count = newVal;
}
public int getCount()
{
return Count;
}
}
This piece of code is also encapsulated, showing that you can have encapsulation without data hiding, but you cannot have data hiding without encapsulation.
{
private int Count;
public Counter()
{
Count = 0;
}
public void setCount( int newVal )
{
Count = newVal;
}
public int getCount()
{
return Count;
}
}
This piece of code is also encapsulated, showing that you can have encapsulation without data hiding, but you cannot have data hiding without encapsulation.
____________________________________________________________________________
Now consider the function Substring() which returns the string based on the start index and length. The underlying algorithm and the internal data structures used to find the substring is specific to the String class and hidden from the external code who makes use of Substring() in which case the external code does not have previliges to modify the algorithm or the datastructures which are internal to (or encapsulated in) String class. This is known as encapsulation.
____________________________________________________________________________
Now consider the function Substring() which returns the string based on the start index and length. The underlying algorithm and the internal data structures used to find the substring is specific to the String class and hidden from the external code who makes use of Substring() in which case the external code does not have previliges to modify the algorithm or the datastructures which are internal to (or encapsulated in) String class. This is known as encapsulation.
____________________________________________________________________________
Links to understand encapsulation:
No comments:
Post a Comment