Encapsulation Example in Java
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
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
age = newValue;updates a variable used in the calculation or output. -
name = newValue;updates a variable used in the calculation or output. -
serialnum = newValue;updates a variable used in the calculation or output. -
Main obj = new Main();updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall 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
.javafilename. - Forgetting semicolons at the end of statements.