English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about encapsulation and data hiding in Java through examples.
Encapsulation is one of the important features of object-oriented programming. Encapsulation refers to binding fields and methods within a single class.
Bundling similar fields and methods within a class also helps in hiding data.
Typically, encapsulation is the process of wrapping similar code in one place.
In Java, we can bundle fields and methods that operate together in a single class. For example:
class Rectangle { int length; int breadth; public void getArea() {} }
In the above program, the getArea() method calculates the area of a rectangle. To calculate the area, it needs the length (length) and width (breadth). Therefore, the data fields length, breadth, and the method getArea() are placed in the Rectangle class.
Data hiding is a method of restricting access to data members by hiding implementation details.
Encapsulation also provides a method to hide data.
Data hiding can be achieved through access modifiers. In Java, there are four access modifiers:
public - The properties or methods decorated are public and can be accessed from anywhere. By default, all properties and methods are public.
private - The properties or methods decorated are private and cannot be accessed from outside the class in which they are declared.
protected - The attributes or methods modified by protected are accessible, similar to private, the difference is that they are also accessible in subclasses
default - That is, without any access modifiers, it is usually called the "default access mode." Under this mode, access is allowed only within the same package.
For more information, please visitJava Modifiers.
class Person { private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } } class Main { public static void main(String[] args) { Person p1 = new Person(); p1.setAge(24); System.out.println("My age is " + p1.getAge()); } }
Output Result
My age is 24
In the above example, we have a private field age. Because it is private, it cannot be accessed from outside the class. To access age, we use public methods getAge() and setAge(). These methods are called getter and setter methods.
If you try to access the age field from the Main class, an error message will appear.
p1.age = 24; // error: age has private access in Person
Setting age to private can limit unauthorized access from outside the class. This is data hiding.
class Person { protected String profession; protected void displayInfo() { System.out.println("I am a " + profession); } } class Teacher extends Person { public static void main(String[] args) { Teacher t1 = new Teacher(); t1.profession = "teacher"; t1.displayInfo(); } }
Output Result
I am a teacher
In the above program, we created a Person class that contains a protected field profession and a protected method displayInfo().
We have accessed these members from the Teacher class (a subclass of Person).
In Java, encapsulation helps us keep related fields and methods together, making our code more organized and easy to read.
It helps to control the modification of our data fields. Consider a situation where we want the age field in the class to be a non-negative number. Here, we can set age to private and apply logic within the setAge() method. For example,
class Person { private int age; public void setAge(int age) { if (age >= 0) { this.age = age; } } }
Getter and setter methods provide read-only or write-only access to our class fields. For example,
getName() // Provide read-only access setName() //Provide write-only access
It helps to decouple the components of the system. These decoupled components can be developed, tested, and debugged independently and simultaneously. And, any changes to a specific component will not have any impact on other components.
People usually think that encapsulation is data hiding, but this is not entirely correct.
Encapsulation refers to binding related fields and methods together.This allows us to achieve data hiding.Encapsulation itself is not data hiding.