Encapsulation:-- Information(Data) hiding.
Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation. As the name suggests, Encapsulation is "hide/enclose something".
Abstraction:-- Implementation hiding.
Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the "abstract form of anything".
class Foo{
private int a, b;
public Foo(){ }
int add(){
return a+b;
}
}
Internal representation of any object of Foo class is hidden outside the class. --> Encapsulation.
Any accessible member (data/method) of an object of Foo is restricted and can only be accessed by that object only.
Implementation of method add() is hidden. --> Abstraction.
Implementation Difference Between Encapsulation and Abstraction
Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier.
Real world example:-
Complex logic is in the circuit board which is encapsulated in a touchpad and a nice interface(buttons) is provided to abstract it out to the user.
So Abstraction is generalized term. i.e. Encapsulation is subset of Abstraction.
Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation. As the name suggests, Encapsulation is "hide/enclose something".
Abstraction:-- Implementation hiding.
Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the "abstract form of anything".
class Foo{
private int a, b;
public Foo(){ }
int add(){
return a+b;
}
}
Internal representation of any object of Foo class is hidden outside the class. --> Encapsulation.
Any accessible member (data/method) of an object of Foo is restricted and can only be accessed by that object only.
Implementation of method add() is hidden. --> Abstraction.
Implementation Difference Between Encapsulation and Abstraction
Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier.
Real world example:-
Complex logic is in the circuit board which is encapsulated in a touchpad and a nice interface(buttons) is provided to abstract it out to the user.
So Abstraction is generalized term. i.e. Encapsulation is subset of Abstraction.
No comments:
Post a Comment