Encapsulation in Java with Example
Overview
In this tutorial, we will learn Encapsulation in Java with an example that we are familiar with. In this example, we will take the Product class.
The product class has three private instance variables:
name, price, quantity.
Let’s take the Product class in this example:
https://www.testingdocs.com/accessor-and-mutator-methods-in-java/
What is Encapsulation?
Encapsulation is hiding the details of the class and binding the implementation with public methods is known as Encapsulation. Java developer allows access of the class to the outside world only through public methods. The public methods of the class are the public interface of the class to the outside world. The private instance methods are hidden and are only manipulated using Setters and Getters methods. ( Mutator and Accessor methods)
Now assume that we allow a new instance variable of the Product class called rating the product and we allow the rating of the product to be displayed on the website. However, the rating of the product should be an integer within the range[0,5] inclusive i.e 0 <= rating <= 5.
private String name;// name of the product private double price;// price of the product private int quantity;// quantity in the store private int rating;// rating of the product [0,5] inclusive
Binding variables
Implementing Encapsulation and binding the variables to the public accessor and mutator methods.
Instance variables are declared private, and hence not visible to outside classes.
For each instance variable in the Product class, we can provide a public Setter (Mutator) method and Getter(Accessor) method.
Instance variable protection
Do not implement or remove the Setter method if we want the variable to be Read-only.
Protect and bind the variables in the Setter method.
In the below example, we protect the rating to be in the valid range [0,5] inclusive using the mutator method.
Without encapsulation, instance variables can be easily corrupted by other classes. For example, rating instance variable valid values are [0,5] and any value is invalid. So we allow the rating to be public, any class can corrupt the value:
private instance variable Protection | Setter(Mutator) Method | Getter(Accessor) Method |
No Access | NO Setter | NO Getter method |
Read-Only Access | NO Setter | Implement Getter method |
Write-Only Access | Implement Setter method | NO Getter method |
Read/Write Access | Implement Setter method | Implement Getter method |
Sample Code
//www.TestingDocs.com public class Product { private String name;// name of the product private double price;// price of the product private int quantity;// quantity in the store private int rating;// rating of the product [0,5] inclusive //Accessor and Mutator for Product rating /** * @return the rating */ public int getRating() { return rating; } /** * @param rating the rating to set * www.TestingDocs.com */ public void setRating(int rating) { if(rating >= 0 && rating <= 5) this.rating = rating; else System.out.println("Error: Invalid Rating"); } } -