Simple way to understand Encapsulation and Abstraction
Learning OOP concepts especially interested to understand Abstraction and Encapsulation in depth.
Checked out the below already
Abstraction VS Information Hiding VS Encapsulation
difference between abstraction and encapsulation?
I found very hard to understand those concepts with out a real and simple example class/code snippet.
One of my colleagues said abstraction is nothing but creating abstract class and normal class that protects its member variable with scope is called Encapsulation.
Is there a simple way I can understand and help others to understand what exactly they are, rather than repeating the below?
Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation focuses upon the implementation that gives rise to this behavior... encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics.
Solution 1:
Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. Consider your mobile phone, you just need to know what buttons are to be pressed to send a message or make a call, What happens when you press a button, how your messages are sent, how your calls are connected is all abstracted away from the user.
Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.
Solution 2:
Abstraction is hiding the information or providing only necessary details to the client.
e.g Car Brakes- You just know that pressing the pedals will stop the vehicle but you don't need to know how it works internally.
Advantage of Abstraction Tomorrow if brake implementation changes from drum brake to disk brake, as a client, you don't need to change(i.e your code will not change)
Encapsulation is binding the data and behaviors together in a single unit. Also it is a language mechanism for restricting access to some components(this can be achieved by access modifiers like private,protected etc.)
For e.g. Class has attributes(i.e data) and behaviors (i.e methods that operate on that data)
Solution 3:
An example using C#
//abstraction - exposing only the relevant behavior
public interface IMakeFire
{
void LightFire();
}
//encapsulation - hiding things that the rest of the world doesn't need to see
public class Caveman: IMakeFire
{
//exposed information
public string Name {get;set;}
// exposed but unchangeable information
public byte Age {get; private set;}
//internal i.e hidden object detail. This can be changed freely, the outside world
// doesn't know about it
private bool CanMakeFire()
{
return Age >7;
}
//implementation of a relevant feature
public void LightFire()
{
if (!CanMakeFire())
{
throw new UnableToLightFireException("Too young");
}
GatherWood();
GetFireStone();
//light the fire
}
private GatherWood() {};
private GetFireStone();
}
public class PersonWithMatch:IMakeFire
{
//implementation
}
Any caveman can make a fire, because it implements the IMakeFire 'feature'. Having a group of fire makers (List) this means that both Caveman and PersonWithMatch are valid choises.
This means that
//this method (and class) isn't coupled to a Caveman or a PersonWithMatch
// it can work with ANY object implementing IMakeFire
public void FireStarter(IMakeFire starter)
{
starter.LightFire();
}
So you can have lots of implementors with plenty of details (properties) and behavior(methods), but in this scenario what matters is their ability to make fire. This is abstraction.
Since making a fire requires some steps (GetWood etc), these are hidden from the view as they are an internal concern of the class. The caveman has many other public behaviors which can be called by the outside world. But some details will be always hidden because are related to internal working. They're private and exist only for the object, they are never exposed. This is encapsulation