Unit 5: Writing Classes
Delving Deep into Java Encapsulation
Introduction to Java Encapsulation
Java encapsulation is one of the four primary principles of Object-Oriented Programming (OOP). It refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit or class. Furthermore, encapsulation is about restricting access to some of the object's components, which is crucial for preventing unintended interference and misuse of data.
The Essence of Encapsulation
Access Modifiers
To implement encapsulation in Java, you use access modifiers. These define the scope and visibility of a class, constructor, variable, method, or data member.
Java supports four access modifiers:
- private: The member is accessible only within its own class.
- default (no modifier): The member is accessible within its own package.
- protected: The member is accessible within its own package and by subclasses.
- public: The member is accessible from any other class.
Using Getters and Setters
To access the private attributes of a class, you typically use getter (accessor) and setter (mutator) methods.
For example, consider a Person class with a private name attribute:
public class Person {
private String name;
// Getter method for name
public String getName() {
return name;
}
// Setter method for name
public void setName(String newName) {
this.name = newName;
}
}
With this setup, you can't directly modify or access the name attribute from outside the Person class. Instead, you'd use the getName() and setName() methods.
Benefits of Encapsulation
- Control: Restricts unauthorized access and modification of data.
- Flexibility and Maintenance: You can change the internal implementation of the class without affecting classes that use it.
- Increased Security: Only methods of that class can set and get the fields, protecting them from being altered by external classes.
Common Mistake
Avoid providing setters for attributes that shouldn't be changed after an object is created, as this would break the encapsulation principle.
Summary
Encapsulation in Java ensures that the data is hidden from outside access and can only be accessed through methods within the class. It helps in protecting the integrity of the data and shields it from unauthorized access and modifications. Grasping encapsulation is pivotal in mastering Java's object-oriented design and programming.
References
- Java Encapsulation with Examples by JavaTpoint
- The Principles of Java Encapsulation by GeeksforGeeks
AP CSA Homework Assignment
Assignment: Implementing Encapsulation in Java
Instructions
- Create a Java class named BankAccount with the following private attributes: accountNumber, accountHolderName, and balance.
- For each attribute, write appropriate getter and setter methods.
- In the setter method for balance, ensure that the balance cannot be set to a negative value. If an attempt is made to set a negative balance, print an error message.
- Write a main method to:
- Create a BankAccount object.
- Set and get its attributes using the methods provided.
- Try setting a negative balance and observe the error message.
After you have completed the task, ensure your code runs without errors and test your program's behavior with various inputs.