Java Encapsulation with Example
Java Encapsulation with Example
Java Encapsulation is often referred to as data hiding. It is a fundamental concept in object-oriented programming that restricts direct access to certain components of an object.
In this tutorial, we will learn Java Encapsulation with an example. 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. It is the wrapping up of variables and methods in one single unit called as class. This means that the internal state of an object is protected from outside interference and misuse.
To achieve encapsulation in Java:
- You can declare the variables of a class as private.
- You can provide public methods to modify and view the variables.
Java developer allows access to 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"); } } -
Java Tutorials
Java Tutorial on this website: