Encapsulation in Java

Introduction

 
In this article, we are going to describe how we can use encapsulation in programming terms with Java. So we first describe the meaning of this very popular concept; encapsulation. And after this, we give the example of how we implement this concept in the Java language.
 

Encapsulation in Java

 
Encapsulation is one of the four fundamental concepts (Inheritance, Polymorphism, Encapsulation, and Abstraction) of OOP. In general, Encapsulation refers to data hiding. Encapsulation in Java is a technique which is used to visible at different places and the Java language itself provides many constructs to encapsulate members. You can completely encapsulate a member be it a variable or method in Java by using the private keyword and you can even achieve a lesser degree of encapsulation in Java by using other access modifiers like protected or public.
 
Encapsulation in Java 
 
Note: The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
 

Benefits of Encapsulation

  • You can achieve data hiding throw encapsulation technique.
  • The fields of a class can be made read-only or write-only.
  • The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.
  • A class can have total control over what is stored in its fields.

How Can We Achieve Encapsulation

 
In the following example, you can see how public methods are the access points to this class's fields from the outside Java world. Normally these methods are referred to as getters and setters. Therefore any class that wants to access the variables should access them through these getters and setters.
 
Example
 
Class: public  EncapExample
  1. public class EncapExample  
  2. {  
  3.     private String collageName;  
  4.     private String colId;  
  5.     private int noStudent;  
  6.     public String getcollageName() {  
  7.         return collageName;  
  8.     }  
  9.     public String getcolId() {  
  10.         return colId;  
  11.     }  
  12.     public int getnoStudent() {  
  13.         return noStudent;  
  14.     }  
  15.     public void setcollageName(String newName) {  
  16.         collageName = newName;  
  17.     }  
  18.     public void setcolId(String newId) {  
  19.         colId = newId;  
  20.     }  
  21.     public void setnoStudent(int no)  
  22.     {  
  23.         noStudent = no;  
  24.     }  
  25. }  
Class : TestEncasulation
  1. public class TestEncasulation  
  2. {  
  3.     public static void main(String args[]) {  
  4.         EncapExample encp = new EncapExample();  
  5.         encp.setcollageName("I.M.S Ghaziabad");  
  6.         encp.setcolId("ims36");  
  7.         encp.setnoStudent(2000);  
  8.         System.out.println("Collage Name : " + encp.getcollageName());  
  9.         System.out.println("Collage ID : " + encp.getcolId());  
  10.         System.out.println("total no of student : " + encp.getnoStudent());  
  11.     }  
  12. }  
OUTPUT
 
Encapsulation in Java  


Similar Articles