- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Encapsulation
Encapsulation is one of the four fundamental pillars of Object-Oriented Programming (OOP). In Java, it refers to the technique of bundling data (variables) and the code that acts on that data (methods) together as a single unit. Think of it as a protective shield that prevents the data from being accessed or modified by code defined outside this "shield."
By using encapsulation, you hide the internal state of an object and only allow interaction through a strictly defined interface. This is often called "Data Hiding."
Definition:
- Encapsulation is the process of wrapping data (attributes) and methods within a single unit, usually a class. It allows a developer to control how data is read or modified, ensuring that an object’s internal state remains consistent and valid.
Private Access Modifier:
- To achieve encapsulation, we use the
privateaccess modifier. When a variable is declared as private, it cannot be accessed directly by any code outside of its own class. - Example:
class Student {
// These fields are hidden from the outside world
private String name;
private int age;
// Direct access to 'name' or 'age' from another class will cause a compilation error.
}
private. Only expose what is absolutely necessary through methods. This keeps your code flexible and prevents "spaghetti code" where every class depends on the internal variables of every other class.
Getter and Setter Methods:
- Since the variables are private, we provide public Getter and Setter methods to allow other classes to interact with the data safely.
- Getters (Accessors) return the value of a variable.
- Setters (Mutators) allow you to update the value, often including validation logic to keep the data "clean."
- Example:
class Student {
private String name;
private int age;
// Getter for name
public String getName() {
return name;
}
// Setter for name
public void setName(String name) {
this.name = name;
}
// Getter for age
public int getAge() {
return age;
}
// Setter for age with validation logic
public void setAge(int newAge) {
if (newAge > 0 && newAge < 120) {
this.age = newAge;
} else {
System.out.println("Please enter a valid age.");
}
}
}
Encapsulation Benefits:
- Data Hiding & Security: Users of the class won't know how the class stores the data. You can perform security checks or data validation inside the setter before the value is actually changed.
- Modularity: The internal implementation of a class can be changed without affecting the parts of the program that use the class. For example, you could change a "fullName" variable into "firstName" and "lastName" internally, while still providing a
getFullName()method for everyone else. - Flexibility & Reusability: Encapsulated classes are easier to test and can be reused across different projects because they are self-contained.
- Control: You can make a class "read-only" (by only providing getters) or "write-only" (by only providing setters), giving you total control over how your data is handled.
Real-World Example: A Bank Account
Imagine a BankAccount class. You wouldn't want any other class to directly change the balance variable to a million dollars. Instead, you encapsulate the balance and only allow changes through a deposit() or withdraw() method that follows specific business rules.
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
Summary
Encapsulation is a fundamental concept of object-oriented programming (OOP) in Java that acts as a contract between your class and the outside world. By using private variables combined with public getters and setters, you ensure code integrity, simplify maintenance, and create robust software systems. Mastering encapsulation is a vital step in moving from writing simple scripts to building scalable, professional Java applications.