In java, Encapsulation means binding object state fields and behavior of methods together. If we start creating a class, it means we are doing encapsulation.
The whole idea behind this encapsulation just hides the implementation details from users. If a data member is private then it means it can only be accessed within the same class. No outside class can access private data members (variable) of other classes.
Main Advantages of encapsulation
- It mainly improves maintainability and flexibility and re-usability for example in the below code the implementation code of void setEmpActor(String name) and String getNameActor() can be changed at any point in time. Since this implementation is purely hidden for outside classes they would still be accessing the private field impactor using the same methods (setEmpActor(String name) and getNameActor()). Hence this code can be maintained at any point in time without breaking the classes that use the code. This also helps to improve the reusability of the underlying class.
- The fields can be made of two things read-only where we don’t define setter methods in the class or write-only where we don’t define the getter methods in the class. For example, If we have a field(or variable) that we don’t want to be changed so we simply define the variable as private and instead of the set and if we just need to define the get method for that variable for getting both. Since the set method is not present and there is no way an outside class can modify the value of that field.
Example :
class EncapsulationTest{ private String empActor; private int empAge; public String getNameActor(){ return empActor; } public int getEmpAge(){ return empAge; } public void setEmpAge(int newValue){ empAge = newValue; } public void setEmpActor(String newValue){ empActor = newValue; } } public class EncapsFinal{ public static void main(String args[]){ EncapsulationTest obj = new EncapsulationTest(); obj.setEmpActor("Ram"); obj.setEmpAge(19); System.out.println("Actor Name: " + obj.getNameActor()); System.out.println("Actor Age: " + obj.getEmpAge()); } } Output : Actor Name: Ram Actor Age: 19
Packages In Java
In Java, Packages are a mechanism to encapsulate a group of classes, sub-packages, and interfaces. Mainly packages in java are used for:
- Preventing naming conflicts.
- Providing controlled access like protected and default have package level access control. A protected member is always accessible by classes in the same package and its subclasses. A default member means without any access specifier is accessible by classes in the same package only.
- Packages also considered as the data encapsulation or you can say data-hiding.
A package is just a container of a group of related classes where some of the classes are accessible are exposed and others are kept for internal purposes. All we need just put related classes into packages. After that, we can simply write an import class from existing packages and directly use it in our program.
Example :
// the Vector class from util package. java.util.vector;
Access Modifiers in Java
The access modifiers in Java it’s used for specifying accessibility or scope of a field, method, constructor, or class. We can easily change the access level of fields, constructors, methods, and class by applying these access modifiers on it.
There are mainly four types of Java access modifiers :
- Private: The access level of this private modifier is only within the class and didn’t access from outside of the class.
- Default: The access level of this default modifier is only within the package and didn’t access from outside the package. Mainly in java, if we did not specify any access level then it will be the default automatically.
- Protected: The access level of this protected modifier is within the package and outside the package through the child’s class. Mainly in java, if we did not make the child class then it cannot be accessed from outside the package.
- Public: The access level of this public modifier is everywhere means it can be accessed from within the class, outside the class, within the package or outside the package.