Home  >  Article  >  Java  >  What is encapsulation and how to use it.

What is encapsulation and how to use it.

WBOY
WBOYOriginal
2024-09-08 16:30:33594browse

What is encapsulation and how to use it.

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:

1. Class Definition with Private Fields
public class Person {
    // Private fields
    private String name;
    private int age;
}

Explanation:

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.

2. Constructor
    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

Explanation:

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.

3. Getter and Setter for
    // Getter for name
    public String getName() {
        return name;
    }

    // Setter for name
    public void setName(String name) {
        this.name = name;
    }

Explanation:

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.

4. Getter and Setter for age (with validation)
    // 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.");
        }
    }

Explanation:

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.

5. Using the Person Class
// 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
    }
}

Explanation:

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:

  • Data Protection:

    The private fields can't be directly accessed or modified from outside the class.

  • Controlled Access:

    Getters and setters provide a controlled way of accessing and modifying the private fields.

  • Validation:

    The setter for age demonstrates how you can enforce rules (e.g., age must be positive) to protect the data from invalid input.

The above is the detailed content of What is encapsulation and how to use it.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Chapter 6 Final TestsNext article:Chapter 6 Final Tests