Ticker

6/recent/ticker-posts

Encapsulation in Java OOPs — Learn with an Example


Encapsulation in Java OOPs — Learn with an Example

Introduction to Encapsulation:
Encapsulation is one of the four fundamental principles of Object-Oriented Programming (OOP) and refers to the bundling of data (attributes) and methods (behaviors) within a single unit, called a class. It helps in hiding the internal implementation details of an object from the outside world, and only exposes the necessary functionalities through well-defined methods. This way, encapsulation promotes data security, code reusability, and maintainability.

Benefits of Encapsulation:

  • Data Hiding: The internal state of an object is hidden from direct access, preventing unauthorized modifications and maintaining data integrity.
  • Abstraction: By exposing only relevant methods, encapsulation allows users to interact with objects without worrying about the internal complexity.
  • Code Reusability: Encapsulated classes can be easily reused in different parts of the program without affecting their internal functionality.
  • Maintainability: Encapsulation makes it easier to modify the internal implementation of a class without impacting the external code that uses it.

Example of Encapsulation in Java:

Let's create a simple example of encapsulation in Java with a class representing a Student and its attributes like name and age, along with methods to access and modify these attributes.

java
public class Student {
// Private attributes - encapsulated data
private String name;
private int age;

// Public methods - encapsulated behaviors
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;
} else {
System.out.println("Invalid age. Age cannot be negative.");
}
}
}

Explanation:
In the above code, we created a class named Student with private attributes name and age. These attributes are encapsulated because they are marked as private, preventing direct access from outside the class.

The class provides public methods, getName(), setName(), getAge(), and setAge(), to interact with the encapsulated attributes. The methods getName() and getAge() allow us to retrieve the values of name and age, respectively. The methods setName() and setAge() are used to modify the values of name and age with certain constraints.

For setAge(), we added a check to ensure that the provided age is a positive value. If the provided age is negative, it displays an error message and does not modify the age attribute.

By encapsulating the attributes and providing controlled access through methods, we ensure that the class maintains data integrity and can modify the internal implementation without affecting the code using the Student class.

Usage:
Now, let's use the Student class in a sample main method:

java
public class Main {
public static void main(String[] args) {
Student student1 = new Student();
student1.setName("Alice");
student1.setAge(20);

System.out.println("Name: " + student1.getName());
System.out.println("Age: " + student1.getAge());

student1.setAge(-5); // Trying to set an invalid age

Student student2 = new Student();
student2.setName("Bob");
student2.setAge(25);

System.out.println("Name: " + student2.getName());
System.out.println("Age: " + student2.getAge());
}
}

Output:

makefile
Name: Alice
Age: 20
Invalid age. Age cannot be negative.
Name: Bob
Age: 25

In this example, we successfully encapsulated the attributes name and age within the Student class, and we used the public methods to access and modify the values in a controlled manner.

Remember that encapsulation is a crucial aspect of OOP, as it promotes better design, security, and maintainability of code. By following the principle of encapsulation, you can write robust and reusable classes in your Java programs.

Post a Comment

0 Comments