Encapsulation is a data hiding mechanism. We achieve encapsulation by making our variables private and providing public getters and setters.
Why do we need to encapsulate? If the property has public access, the client code can have direct access and can assign any value. By encapsulating we have one layer where we can control what comes to our property in our setter method. In the above example, we can see how we are restricting negative age in the setAge method. Another example — let’s say we are creating a custom List data structure based on an array. The underlining array data structure should be private if we will make it public, it will be accessible to client code. There are possibilities that client code will create a big mess by manipulating directly with the array.
From whom we are protecting our data? Seems a very simple question however understanding this question is the main part to start developing using the encapsulation concept. We are removing direct access from object properties so we are protecting it from client code that will use this class(object). We are hiding properties from ourselves if we will use this class in other parts of the project.
If the property type of object is a mutable object. We cannot return the original address because the client will direct access using the returned reference. We always need to take a copy and return a reference to it.
The main idea of the above encapsulation to make sure only available toppings can be assigned to our toppings list. But there is an issue with this code. It returns toppings
directly. It’s a reference so using this reference client code could change toppings
directly.
We were able to add toppings that are not part of the available toppings. Even we could add null. So we want to achieve incapsulation correctly we never return references directly instead we return their copies.
For example, we can fix our Pizza
class by changing getToppings
method
public List<String> getToppings() {
return new ArrayList<>(toppings); // return copy
}
Even though you can create your setters and getters methods with any name or if the requirement is not required to have them, it is totally fine to avoid them. But keep in mind that if you want to use your objects with external libraries, they might assume you have all setters and getters with correct names.
That’s all for encapsulation in Java.
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET