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.
Java uses the private
modifier to limit the visibility of class attributes. A private
attribute can be accessed only within its specific class.
Java1public class MyRobot { 2 private String robotName; // This attribute can be accessed within the MyRobot class only 3}
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, 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.
Java1public class MyRobot { 2 private String robotName; 3 4 public String getRobotName() { // Getter method for RobotName 5 return robotName; // Returns current robot name 6 } 7}
Setters are used to change (or set) the value of a private attribute.
Java1public class MyRobot { 2 private String robotName; 3 4 public void setRobotName(String newRobotName) { // Setter method for RobotName 5 this.robotName = newRobotName; // Sets the robot name to new value 6 } 7}
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.
Let's consolidate these insights with an example.
Java1public class MyRobot { 2 private String robotName; 3 4 public String getRobotName() { 5 return this.robotName; // Fetches current robotName 6 } 7 8 public void setRobotName(String newRobotName) { 9 this.robotName = newRobotName; // Changes current robotName to new input 10 } 11} 12 13MyRobot robot = new MyRobot(); // Initialize an object, bot, of the class MyRobot 14// robot.robotName = "R2-D2"; // This fails, you can't access a private field directly 15robot.setRobotName("Bot-202"); // Sets robotName to "Bot-202" 16System.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.
Great job! Today, you navigated through the concept of encapsulation in Java, leveraging getters, setters, and the private
keyword to build secure and reliable code.
Remember our robot? Essentially, we constructed a bot, secured the core (private attributes), and installed controlled access points (getters and setters). Now, you're ready to put them into action. I look forward to seeing you in the practice exercises next! Happy learning!