Encapsulation in Java: An Overview

Hello! Today's lesson explores encapsulation in Java, a process that keeps sensitive class data safe through controlled access. Think of it as a safety box for class attributes. By implementing encapsulation, we can maintain the integrity of our data and the security of our code.

So, what is encapsulation? It's essentially data (attributes) and code arising from this data (methods) wrapped together to create a 'protective shell'. The class attributes become highly secure because they can only be accessed through the class methods. In other words, encapsulation keeps your data safe, ensuring your code is secure and reliable.

Understanding the 'private' Keyword in Java

Java uses the private modifier to limit the visibility of class attributes. A private attribute can be accessed only within its specific class.

public class MyRobot {
    private String robotName; // This attribute can be accessed within the MyRobot class only
}

In the MyRobot class mentioned above, robotName is a private attribute, which implies it can be directly accessed only within the MyRobot class.

Getters and Setters in Java

Getters and setters, also known as accessors and mutators, respectively, allow us to read and modify private attributes from outside the class.

Getters are used to obtain (or get) the value of a private attribute.

public class MyRobot {
    private String robotName;
    
    public String getRobotName() { // Getter method for RobotName
        return robotName;  // Returns current robot name
    }
}

Setters are used to change (or set) the value of a private attribute.

public class MyRobot {
    private String robotName;
    
    public void setRobotName(String newRobotName) { // Setter method for RobotName
        this.robotName = newRobotName;  // Sets the robot name to new value
    }
}

Getters and setters form a controlled passage to access and modify the object's hidden internal state. If you want your private variable to be non-editable - you don't include setter in your class, if you don't want it to be accessible - you don't include getter.

Putting It All Together: Encapsulating Class Attributes and Methods

Let's consolidate these insights with an example.

public class MyRobot {
    private String robotName;
    
    public String getRobotName() {
        return this.robotName;  // Fetches current robotName
    }
    
    public void setRobotName(String newRobotName) {
        this.robotName = newRobotName;  // Changes current robotName to new input
    }
}

MyRobot robot = new MyRobot(); // Initialize an object, bot, of the class MyRobot
// robot.robotName = "R2-D2"; // This fails, you can't access a private field directly
robot.setRobotName("Bot-202"); // Sets robotName to "Bot-202"
System.out.println(robot.getRobotName());  // Gets robotName and prints it

In this example, setRobotName() sets a new name for our MyRobot object while getRobotName() retrieves the current name. You can modify and access robotName in a controlled, safe manner.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal