Encapsulation in Java programming means hiding specific properties of an object inside while enabling some properties outside.
What is Encapsulation in Java?
Capsulate means capsule-like.
One crucial advantage of hiding or exposing (encapsulating) specific properties in Java is that it protects the object from external, unwarranted access.
Generally, during the creation of any object, neither all its properties cannot be hidden or exposed. For example, while a watch necessitates the exposure of the seconds and minutes hands, other properties, such as the battery, are hidden inside.
Precisely ENCAPSULATION Means
The process of selective hiding or selective exposure of certain properties of an object that are relevant to the specification is known as encapsulation in Java.
Here, API is the specification.
API helps to understand the package, what is inside the class and interface, and what methods, data members, and constructors they contain.
Note: The programmer can express all the properties of an object in a class.
Programmers should understand data abstraction to implement the concept of encapsulation in Java. Before understanding encapsulation, we will briefly discuss abstraction and abstract ideas.
Difference between Abstract and abstraction
Abstract:
Abstract means something hidden, unseen, vague, incomplete, or unclear.
Example 1: Imagine an instructor and student connected during an online session. The instructor cannot see the brand of the student’s laptop. In other words, the brand of the student’s laptop is hidden or unseen from the instructor.
Abstraction:
Abstraction is a word that has a different meaning from abstract.
It means acquiring information from an object without going into the detailed aspect of its implementation.
Example 2: To understand the meaning of abstraction, we will take a mobile phone as an example. When purchasing a new mobile phone, the manufacturer provides a manual to help the customer learn about the device’s operation.
Then, the customer checks the device to see whether it works properly, as mentioned in the manual, by operating it rather than just seeing it. However, we do not open the device to see whether all the components are correctly connected. Likewise, abstraction involves getting an object’s information.
Data Abstraction Meaning
Data abstraction is the process of acquiring information from an object relevant to the specification without going into the detailed implementation. In programming, implementation means method, body, or context.
Data Abstraction in Object-Oriented Programming:
Data abstraction is a unique feature of object-oriented programming languages.
How to achieve Encapsulation in Java?
Through access specifiers, certain properties in Java can be hidden, and some can be exposed. For instance, the public access specifier makes specific properties public; in contrast, the private access specifier helps hide properties.
What are Access Specifiers in Java?
To implement encapsulation, we use access specifiers. There are four kinds of access specifiers in Java; they include,
- public
- private
- protected
- default
If we don’t use the first three access specifiers, JVM will assign a default access specifier.
Example Program using Encapsulation in Java
To know what access specifiers are in Java, click on the link.