Encapsulation in Java Last Updated : 21 Nov, 2025 Comments Improve Suggest changes 456 Likes Like Report Encapsulation means combining data and the functions that work on that data into a single unit, like a class. In Object-Oriented Programming, it helps keep things organized and secure.A class can hide the implementation part and discloses only the functionalities required by other classes. By making class data and methods private, representations or implementations can later be changed without impacting the codes that uses this class.It helps in better maintainability, readability and usability. It also helps maintain data integrity by allowing validation and control over the values assigned to variables.EncapsulationImplementation of Encapsulation in JavaDeclare data as private: Hide the class data so it cannot be accessed directly from outside the class. Use getters and setters: Keep variables private and provide public getter and setter methods for controlled access and safe modification, often with validation.Apply proper access modifiers: Use private for data hiding and public for methods that provide access. Java class Programmer { private String name; // Getter method used to get the data public String getName() { return name; } // Setter method is used to set or modify the data public void setName(String name) { this.name = name; } } public class Geeks { public static void main(String[] args){ Programmer p = new Programmer(); p.setName("Geek"); System.out.println("Name=> " + p.getName()); } } OutputName=> Geek Explanation: In the above example, we use the encapsulation and use getter (getName) and setter (setName) method which are used to show and modify the private data. This encapsulation mechanism protects the internal state of the Programmer object and allows for better control and flexibility in how the name attribute is accessed and modified.Advantages of EncapsulationThe advantages of encapsulation are listed below:Data Hiding: Encapsulation restricts direct access to class variables, protecting sensitive data from unauthorized access.Improved Maintainability: Changes to internal implementation can be made without affecting external code that uses the class.Enhanced Security: Encapsulation allows validation and control over data, preventing invalid or harmful values from being set.Code Reusability: Encapsulated classes can be reused in different programs without exposing internal logic.Better Modularity: Encapsulation promotes organized, modular code by keeping data and methods together within a class.Disadvantages of EncapsulationThe disadvantages of encapsulation are listed below:Increased Code Complexity: Writing getter and setter methods for every variable can make the code longer and slightly more complex.Performance Overhead: Accessing data through methods instead of directly can introduce a minor performance cost, especially in performance-critical applications.Less Flexibility in Some Cases: Over-restricting access to class members may limit the ability of other classes to extend or use the class efficiently. Create Quiz Encapsulation with Java Visit Course Comment H Harsh Agarwal 456 Improve H Harsh Agarwal 456 Improve Article Tags : Java Java-Object Oriented Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like