Skip to main content

Encapsulation Example in Java

2 min read Updated May 29, 2026
Share:
On this page (9sections)

Introduction

Encapsulation is a classic Java console program that demonstrates the concept with complete source code and sample output. Object-oriented programming models real entities with classes, objects, inheritance and polymorphism.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

If a class disallows calling code from accessing internal object data and forces access through methods only, this is a strong form of abstraction or information hiding known as encapsulation. This is useful because it prevents the external code from being concerned with the internal workings of an object. This facilitates code refactoring, for example allowing the author of the class to change how objects of that class represent their data internally without changing any external code.

Encapsulation Example Program

class Main{
    private int serialnum;
    private String name;
    private int age;
    public int getEmpserialnum(){
        return serialnum;
    }
    public String getEmpName(){
        return name;
    }
    public int getEmpAge(){
        return age;
    }
    public void setEmpAge(int newValue){
        age = newValue;
    }
    public void setEmpName(String newValue){
        name = newValue;
    }
    public void setEmpSSN(int newValue){
        serialnum = newValue;
    }
}
public class EncapsulationDemo{
    public static void main(String args[]){
        Main obj = new Main();
        obj.setEmpName("XYZ");
        obj.setEmpAge(32);
        obj.setEmpSSN(3121222);
        System.out.println("Employee Name: " + obj.getEmpName());
        System.out.println("Employee Serial number: " + obj.getEmpserialnum());
        System.out.println("Employee Age: " + obj.getEmpAge());
    } 
}

Sample Output

Employee Name: XYZ
Employee Serial number: 3121222
Employee Age: 32

When to use

Use OOP examples when modelling entities with state and behaviour in larger applications.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. age = newValue; updates a variable used in the calculation or output.

  3. name = newValue; updates a variable used in the calculation or output.

  4. serialnum = newValue; updates a variable used in the calculation or output.

  5. Main obj = new Main(); updates a variable used in the calculation or output.

  6. A println / print call writes text to the console — part of the sample output below.

  7. A println / print call writes text to the console — part of the sample output below.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Use meaningful variable and class names that describe their purpose.
  • Compile and run the program locally — modify values to see how output changes.
  • Read compiler errors carefully; they usually point to the exact line to fix.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Encapsulation program demonstrate?
It shows how to implement encapsulation in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use OOP examples when modelling entities with state and behaviour in larger applications.

Related Tutorials

Search tutorials