Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Encapsulation is one of the four fundamental principles of Object-Oriented Programming (OOP) and is used to hide the internal state and implementation details of an object. It allows an object to expose a simple and consistent interface while keeping its internal state and logic hidden from the outside world. Encapsulation is achieved using access modifiers (private, protected, and public) and getter and setter methods.
In this tutorial, we will go through an example demonstrating encapsulation in Java.
Consider a simple Person
class:
public class Person { private String name; private int age; // Constructor public Person(String name, int age) { this.name = name; this.age = age; } // Getter methods public String getName() { return name; } public int getAge() { return age; } // Setter methods public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } }
Here, the Person
class has two private fields: name
and age
. These fields are hidden from the outside world, and their values can only be accessed and modified through the provided getter and setter methods.
The private access modifier restricts access to the fields and methods within the class. In the Person
class, the name
and age
fields are marked as private, which prevents them from being accessed directly from outside the class.
Getter methods are public methods that return the value of a private field. In the Person
class, we have two getter methods, getName()
and getAge()
, that allow us to access the private fields name
and age
, respectively.
Setter methods are public methods that set the value of a private field. In the Person
class, we have two setter methods, setName()
and setAge()
, that allow us to modify the private fields name
and age
, respectively.
By using encapsulation, we can control how the internal state of the Person
class is accessed and modified. This approach provides several benefits:
In summary, encapsulation is a crucial principle in OOP that helps to create well-structured and maintainable code by hiding the internal state and implementation details of a class.
Java Encapsulation Example: Encapsulation involves bundling data and methods that operate on the data within a single unit, typically a class.
public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if (age >= 0) { this.age = age; } } }
Encapsulation in Java with Private and Public Modifiers: Use private modifiers to restrict direct access to class fields and public modifiers for controlled access through methods.
private String name; // private field public String getName() { // public getter return name; }
How to Achieve Data Hiding in Java Classes: Data hiding is achieved by marking fields as private and providing public methods for access.
private String sensitiveData; // Hidden data public String getSensitiveData() { // Controlled access return sensitiveData; }
Java Getters and Setters for Encapsulation: Getters retrieve the value of a private field, and setters modify its value, providing controlled access.
private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }
Encapsulation and Information Hiding in Java: Information hiding is a key aspect of encapsulation, preventing direct access to internal details.
private String secretInfo; // Hidden information public String getSecretInfo() { // Controlled access return secretInfo; }