What is encapsulation?
Encapsulation in Java is all about keeping the details of how something works hidden while still letting others use it. You group your data (like variables) and methods (like functions) into one unit, called a class. Instead of letting everyone directly access your data, you provide methods (getters and setters) to control how it’s accessed or changed. This way, you protect your data and keep your code clean and organized, without letting anyone mess with the inner workings unless you want them to.
How to use it
To use encapsulation in Java, you create a class with private fields and provide public methods (like getters and setters) to access and modify those fields. This ensures that the data can only be changed in a controlled way. For example, if you want to create a Person class where the name and age are private, you would use getters to retrieve the values and setters to update them. Here's how you can do it:
public class Person { // Private fields private String name; private int age; // Constructor public Person(String name, int age) { this.name = name; this.age = 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 public void setAge(int age) { if(age > 0) { // Simple validation this.age = age; } else { System.out.println("Age must be positive."); } } } // Using the Person class public class Main { public static void main(String[] args) { Person person = new Person("John", 25); // Accessing fields using getters System.out.println(person.getName()); // Output: John System.out.println(person.getAge()); // Output: 25 // Modifying fields using setters person.setName("Jane"); person.setAge(30); System.out.println(person.getName()); // Output: Jane System.out.println(person.getAge()); // Output: 30 } }
Lets break it down
Let's break down the code and explain each section step by step:
public class Person { // Private fields private String name; private int age; }
This is the Person class where we define two private fields: name (a String) and age (an int). By making these fields private, we ensure that no other class can directly access or modify them. This is the core idea of encapsulation — hiding the internal state of an object.
// Constructor public Person(String name, int age) { this.name = name; this.age = age; }
The constructor initializes the Person object when it's created. It takes two parameters, name and age, and assigns these values to the private fields. This ensures that when a new Person object is created, it starts with valid data.
// Getter for name public String getName() { return name; } // Setter for name public void setName(String name) { this.name = name; }
The constructor initializes the Person object when it's created. It takes two parameters, name and age, and assigns these values to the private fields. This ensures that when a new Person object is created, it starts with valid data.
// Getter for age public int getAge() { return age; } // Setter for age public void setAge(int age) { if (age > 0) { // Simple validation this.age = age; } else { System.out.println("Age must be positive."); } }
The getter getAge() works the same way as the one for name, allowing access to the age field.
The setter setAge() not only allows modification of the age field but also adds a validation check. The if condition ensures that the age is only set if it's a positive number. If an invalid age is provided (like a negative number), the setter prevents the update and prints a message instead. This is an example of how encapsulation lets you control what kind of data can be set.
// Using the Person class public class Main { public static void main(String[] args) { Person person = new Person("John", 25); // Accessing fields using getters System.out.println(person.getName()); // Output: John System.out.println(person.getAge()); // Output: 25 // Modifying fields using setters person.setName("Jane"); person.setAge(30); System.out.println(person.getName()); // Output: Jane System.out.println(person.getAge()); // Output: 30 } }
In this section, we create a Person object (person) using the constructor. The constructor sets the initial name as "John" and age as 25.
Then, we use the getters (getName() and getAge()) to print the values of name and age. After that, we use the setters (setName() and setAge()) to update the values. Finally, the updated values are printed using the getters again.
Key Points of Encapsulation in Action:
The private fields can't be directly accessed or modified from outside the class.
Getters and setters provide a controlled way of accessing and modifying the private fields.
The setter for age demonstrates how you can enforce rules (e.g., age must be positive) to protect the data from invalid input.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3