Encapsulation in java
Encapsulation
Using Encapsulation we achieve “Data Hiding, Information Hiding” in java. Encapsulation binds data (attributes and methods) and the code in the form of a class. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. Encapsulation provides “access control” through which we can control the access of class in accessing methods or members of class.
Encapsulation is performed by constructing the class. Abstraction is achieved by creating either Abstract Classes or Interfaces on top of your class.
Real world examples of Encapsulation:
- Washing Machine and its button on machine which turns the machine “ON”: Function of button is to start the functioning of machine but did you ever think that what happens inside machine when button was pressed like inside mechanism of machine. Actually we do not need to worry about that internal functionality of washing machine till the time our machine is working fine that is called encapsulation. You don’t "need to know the internal working of the washing machine to operate" with it. You have an interface to use the device behavior without knowing implementation details.
- Car and self of car which starts the engine of car: Function of self is to start the engine of car but did you ever tried to find out what happens when self is moved using key in the car and how car’s engine got started by using self. Again we do not need to worry about the inside functionality of car when car stops working we will visit showroom to correct the problem why car is not getting started using self this is called encapsulation.
- Capsule given to patient by doctor: Doctors give capsules to patients which can be a mixture of many different medicines patient do not know which medicines are present in capsule but they get relief with the capsule given by doctor this is called encapsulation.
- Bank Account: When you visit bank to withdraw amount from your account you just need to share your account number with bank employee with you signatures on the withdrawal form have you ever tried to find out how internal process works this is called encapsulation.
- Mobile Phone: When we use mobile phone we just type in the number on our keypad to have conversation with the other person but we do not know the internal working that how call is being made to the user this is called encapsulation.
If you still have any question you can reach me any time through email
(jigyasu2010@hotmail.com)
Comments
Post a Comment