In our last chapter we discussed about access specifiers. And we discussed three basic types of access specifiers or access modifiers, including public, private and protected.
Restricting access to some or all properties and methods of a class or object is very important. Because, sometime we don’t want to give a direct access to some (or all) information (or data) stored in instance variables of an object to another object.
Let’s take an inventory management program again as an example to better understand this. In our program, we have a class ‘Product’ and our class has different properties in it, that includes name, sku, price and category.
Now I don’t want any other object to directly access any of this information stored in object’s of ‘Product’ class. Let’s assume we only have one object of ‘Product’ class in your program. i.e. Product A. What would I do? Yes, I will declare all of these properties as private.
Now let’s assume I have initialized my object ‘Product A’ using a parameterized constructor and my ‘Product A’ has some information already stored in it.
// Product class definition in Java
public class Product {
private String name;
private String sku;
private double price;
private String category;
// Constructors like class can't
// be declared as private.
public Product (String name, String sku,
double price, String category);
}
// Somewhere in our program we will
// initialize our object like this.
Product product_A = new Product(
"Product A",
"C1-PA-3456",
2000.00,
"Category A");
Now let’s assume during execution of my program, we want to change category of our product for some reason. What would I do again? Am I missing something? Yes, I should have declared getter and setter for private properties of my ‘Product’ class. And I will make those public to make those publicly available anywhere in my program. Let’s do this now.
// Product class definition in Java
public class Product {
private String name;
private String sku;
private double price;
private String category;
// I am not writing complete code
// for get and set methods
// to save time.
public String getName();
public void setName(String name);
public String getSku();
public void setSku(String sku);
public double getPrice();
public void setPrice(double price);
public String getCategory();
public void setCategory(String name);
// Constructors like class can't
// be declared as private.
public Product (String name, String sku,
double price, String category);
}
// Somewhere in our program we will
// initialize our object like this.
Product product_A = new Product(
"Product A",
"C1-PA-3456",
2000.00,
"Category A");
// We can change category using set
// method anywhere in a java
// program like this
product_A.setCategory("Category B");
This concept of restricting direct access to information stored in instance variables of an object is known as ‘Encapsulation’. Or in other words.
Encapsulation is to make sure that information or data of an object is completely hidden and secure from other objects. And it can only be read and modified using available public methods of that object or class if required.
If you still have any confusion on this, please ask your questions as comments below.
Let’s move on to our next chapter. Please click on ‘Next’ to continue.