0
Can someone explain the difference between encapsulation and abstraction?in very very simple language..
if u know Hindi please use Hindi otherwise English..
3 Antworten
+ 11
Let's see.
You have money. It belongs to you, and only you can use it. If other people want to borrow some money, they have to ask for your permission. They cannot directly take and use your money. In order to do so, they have to go through you. This is similar to the idea of encapsulation.
class Bahubali()
{
private: int money;
public:
int borrow_money()
{
std::cout << "Borrowing money."
money -= 50;
return 50;
}
};
Let's say that you want to create a method for people to earn money using do_jobs(), but the user will have to pass a lot of information to the method in order to use it, e.g.
do_jobs(name, age, phone_num, position, start_date, end_date, is_employed, session_id, job_id, task_num);
These parameters should be irrelevant, from a user's perspective, but you need it to store a record in your database. Users still find it hard to use this method, so you write an overload of the method, and provide an abstraction which handles all the information. Users can now just do:
do_jobs(identity);
and all the required information is automatically handled, not exposed directly to the user.
+ 2
In simple terms, abstraction is the representation of information attributes without including the explanations. It is mainly used in the classes where data members such as age are simply listed without further details.
On the other hand, encapsulation is simply the process of restricting the access of data members and functions by using access modifiers such as private, protected or public depending on the access required.