ENCAPSULATION Object Oriented Programming is a paradigm that

ENCAPSULATION Object Oriented Programming is a paradigm that provides many concepts such as encapsulation, inheritance, polymorphism, abstraction. The programming paradigm where everything is represented as an object, is known as truly object-oriented programming language. Object-oriented programming tries to model real-life objects into the programming world. This makes the programs easy to co-relate with real-world and understand. Encapsulation is one of the key concepts which plays crucial role in this modeling, along with inheritance, polymorphism, and abstraction.

ANALYZING REAL-LIFE OBJECTS What we expect from real-life objects is behavior. Take a car as an example. What are expectations from Car? Start the car. Accelerate more n more. Break. Move to reverse slowly. Stop. To achieve these behaviors, you are provided with some of the mechanisms. Ignition to start and stop the car. Gearbox, accelerator and break to increase or decrease the speed of car. You use these accelerator and break to achieve the desired speed of the car.
![1 class Main{ 2 public static void main(args String[]){ 3 Car car = new 1 class Main{ 2 public static void main(args String[]){ 3 Car car = new](http://slidetodoc.com/presentation_image_h2/ae60082a76637907b90816e91475333f/image-3.jpg)
1 class Main{ 2 public static void main(args String[]){ 3 Car car = new Car(); 41 class car. is. Started = true; Car{ 52 public car. set. Speed 10; float speed= =0; 63 public car. set. Speed = 20; boolean is. Reverse = false; 74 public car. set. Speed = 30; boolean is. Started = false; 85 } car. set. Speed = 20; 9 car. set. Speed = 10; 10 car. set. Speed = 0; 11 car. is. Reverse = true; 12 car. set. Speed = 10; 13 car. set. Speed = 0; 14 car. is. Started = false; 15 }

English meaning of encapsulation, §To encase in or as if in a capsule §To express in a brief summary Technical translation of encapsulation, §Wrapping of data in a construct such that it hides its internal data §Provide behavior methods to control access of data in desired fashion
![SAMPLES OF CLASSES CLIENT A class Main{ public static void main(args String[]){ Car car SAMPLES OF CLASSES CLIENT A class Main{ public static void main(args String[]){ Car car](http://slidetodoc.com/presentation_image_h2/ae60082a76637907b90816e91475333f/image-5.jpg)
SAMPLES OF CLASSES CLIENT A class Main{ public static void main(args String[]){ Car car = new Car(); // No need to start? ? car. speed = 100; // Turbo mode directly to 100 car. speed = 0; // Turbo break } CLIENT B class Main{ public static void main(args String[]){ Car car = new Car(); car. is. Started = true; car. is. Reverse = true; car. set. Speed = 100; }

WHAT IS ENCAPSULATION? Encapsulation means the localization of the information or knowledge within an object. Encapsulation is also called as “Information Hiding”. 1) Objects encapsulate data and implementation details. To the outside world, an object is a black box that exhibits a certain behavior. 2) The behavior of this object is what is useful for the external world or other objects. 3) An object exposes its behavior by means of methods or functions. 4) The set of functions an object exposes to other objects or external world acts as the interface of the object.

HOW IS ENCAPSULATION DONE IN JAVA? Java provides 4 different access modifiers to control visibility of internal data from outside world. private, protected, default(no access modifier) and public. private – Most restrictive, allows only class level access. default - Package level access protected – Package level access + access to subclasses. public – Least restrictive, doesn’t impose any visibility constraint.

BENEFITS OF ENCAPSULATION The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code. 1) Makes it easy to model real-world entities – hence easy to understand maintain 2) Control the way data is accessed or modified --The functionality where we can change the implementation code without breaking the code of others who use our code is the biggest benefit of Encapsulation. 3) Makes the class easy to use for clients -- Here in encapsulation we hide the implementation details behind a public programming interface. By interface, we mean the set of accessible methods our code makes available for other code to call—in other words, our code’s API. 4) Increase reusability and aids to the flexibility of design -- By hiding implementation details, We can rework on our method code at a later point of time, each time we change out implementation this should not affect the code which has a reference to our code, as our API still remains the same
- Slides: 8