Encapsulation in Java with Example

< Previous Next >

OOPS Tutorial

1. Overview

In this post, we will learn the important object-oriented concept Encapsulation.
Encapsulation refers to combining data and associated methods as a single unit. In this post, we will learn Encapsulation in detail with examples.

2. Intent/Definition

Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safe from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper.

In OOP, data and methods operating on that data are combined together to form a single unit, which is referred to as a Class. Read more about Java Class here.

Advantage of Encapsulation

The main advantage of using encapsulation is to secure the data from other methods. For example, when we make a fields private then these fields only use within the class, but these data not accessible outside the class.

Real-world examples

  1. Capsule, it is wrapped with different medicines. In a capsule, all medicine is encapsulated inside a capsule.
  2. A Java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

3. Implementation

The process of binding or wrapping the data and the codes that operate on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
For example - if a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class.
Consider below Person class diagram, the id and name parameters should not be accessed directly outside Person class - achieved by private declaration.

Let's create a Person class to demonstrates the use of encapsulation in Java.

Step 1: Create a Person class.
public class Person {

    private double id;
    private String name;

    public Person() {
        // Only Person class can access and assign
        id = Math.random();

    // This method is protected for giving access within Person class only
    private void sayHello() {
        System.out.println("Hello - " + getId());

    public double getId() {
        return id;

    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
Step 2: Let's test the Encapsulation via a main() method.
public class EncapsulationDemostration {
    public static void main(String[] args) {

        Person p1 = new Person();
         * Note: As id and name are encapsulated in Person class, those cannot be accessed 
         * directly here. Also there is no way to assign id in this
         * class. Try to uncomment below code and you would find compile time error.
        // p1.id = "123";
        // p1.name = "this will give compile time error";
        // p1.sayHello(); // You can't access this method, as it is private to Person

        System.out.println("Person 1 - " + p1.getId() + " : " + p1.getName());

4. Difference between Abstraction and Encapsulation

Abstraction and Encapsulation in Java are two important Object-oriented programming concept and they are completely different from each other.
  • Encapsulation is a process of binding or wrapping the data and the codes that operate on the data into a single entity. This keeps the data safe from outside interface and misuse.
  • Abstraction is the concept of hiding irrelevant details. In other words, make the complex system simple by hiding the unnecessary detail from the user.
  • Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifiers.
  • Abstraction solves the problem at the design level. Whereas Encapsulation solves the problem at the implementation level.
Encapsulation: Information hiding.
Abstraction: Implementation hiding.

GitHub Repository

The source code of this article available on my GitHub repository at OOPS Concepts in Java.

Learn complete Java Programming with Examples

Related Oops Posts

Top Core Java Tutorials

  1. Java Tutorial for Beginners
  2. 50 Java Keywords
  3. JDBC 4.2 Tutorial
  4. All Java/J2EE Tutorial
  5. Java 8 Tutorial
  6. Java Collections Tutorial
  7. Java Exceptions Tutorial
  8. Java Generics Tutorial
  9. Java 8 Stream API Tutorial
  10. Java Wrapper Classes
  11. Java Arrays Guide
  12. Java Multithreading Tutorial
  13. Java Concurrency Tutorial
  14. Oops Concepts Tutorial
  15. Java String API Guide
  16. Java Reflection API Tutorial
  17. Java I/O Tutorial
  18. Date and Time API Tutorial
  19. JUnit 5 Tutorial
  20. JUnit 4 Tutorial
  21. Java XML Tutorial
  22. Google GSON Tutorial

Top Java EE Tutorials

  1. Spring Security Tutorial
  2. RabbitMQ Tutorial
  3. Hibernate ORM 5
  4. Java Persistence API
  5. Spring Boot 2 Tutorial
  6. Spring Core 5 Tutorial
  7. Spring MVC 5 Tutorial
  8. Spring Data JPA Tutorial
  9. Apache HttpClient Tutorial
  10. Spring Framework 5
  11. Apache Maven Tutorial
  12. JAX-RS Tutorial
  13. Jersey Rest Tutorial

Java Library Tutorials

  1. Java API Guides
  2. Java SQL Package Tutorial
  3. All Java/J2EE Tutorial
  4. Java Lang Package Tutorial
  5. Java Util Package Tutorial
  6. Java Lang Reflect Package Tutorial
  7. Java Time Package Tutorial
  8. Java IO Package Tutorial

Top Java Best Practices

  1. Java Enums and Annotations Best Practices
  2. Java Generics Best Practices
  3. JUnit Framework Best Practices
  4. Java Naming Conventions
  5. Single Responsibility Principle
  6. Liskov's Substitution Principle
  7. Interface Segregation Principle
  8. Dependency Inversion Principle
  9. Open Closed Principle
  10. Oops principles in java
  11. Restful API Best Practices
  12. JSP Best Practices
  13. Guide to JDBC Best Practices
  14. Collection Best Practices
  15. String Best Practices in Java
  16. Exception Handling Best Practices
  17. Synchronization Best Practices
  18. Guide to JDBC Best Practices
  19. Serialization Best Practices



Post a Comment