Saturday, January 15, 2011

Encapsulation

Encapsulation
The concept of sealing data and methods into an object is known as encapsulation. Its ability is to hide information and internal implementation from the users. Information hiding doesn’t mean to hide information but it means that it gives limited access to user. Access modifiers provides accessibility level for user.



Examples
If you want to meet the president of your country you can’t go directly to meet president. Guard or Security of president will check you and if you have appointment or you have access then you can go to meet him. That’s what encapsulation does. Its guard your data not hides it.  

Take an example of car. All of us drive car when we sit in car and insert key in it and turn it to start the car , the car get started but we don’t know how its starts. We don’t know the internal working of the car. The internal implementation is hidden from the user.


A tracker in a car that helps us trace our car when someone steals our car, so we just let the tracking service provider company know that my car has been stolen I know they can trace it but keep it hidden how are they going to trace it 

Public Access Modifier


We can only access Name because it is public.

Private Access Modifier
To access private variable we have to make property for that variable which will guard the variable and we can access private variable via that property.

Now we can access EngineNo variable via EngineNo1 property.





Protected Access Modifier
Protected variables can be access by inherited class. Let make another class which will inherit clsCar.




No comments:

Post a Comment