Encapsulation in Java OOPs with Example

Encapsulation is an act of combining properties and methods of the same entity. In other words, it is a process of wrapping the data (variables) and code together in a single unit. It is an OOPs concept. Encapsulation is also known as data hiding because the data in a class is hidden from other classes. A capsule is an example of encapsulation which is mixed of several medicines.

Encapsulation in Java OOPs with Example

In Java, we can create a fully encapsulated class by declaring all the data members (variables) of the class private. If the data member is private then it can be only accessible within the same class. No other class can access the data member of that class.



Now the question arises if we are declaring all the data member (variables) of the class private then how should we use this data member. So the answer is we can use setter and getter method to set and get the value of data member (variable) of the class private. We can implement encapsulation in java by using JavaBean class. The following code is the example of a getter and setter method.

package com.javastudypoint;
class Employee{
 private int id;
 private String name;
 public void setName(String name){
  this.name = name;
 }
 public String getName(){
  return name;
 }
}


Advantages of Encapsulation.

  • Encapsulation is a way to achieve the data hiding in java so that the other class will not be able to access the private members of the class.
  • In Encapsulation, we can hide the internal information of the data which is better for security concern.
  • With Java Encapsulation, we can make the class read-only i.e. A class which has only getter methods and write-only i.e. A class which has only setter methods. If we don't want to change the value of a variable, we can use the read-only class. If we want to change the value of a variable, we can use the write-only class.
  • In Encapsulation, we can combine the variables and methods in a single unit. So that the encapsulation provides control over the data.


Example of Encapsulation in Java:

In this example, we are going to show you how to achieve Encapsulation in Java. To achieve Encapsulation in Java we need private member and getter and setter method. setter and getter method set and get the value of data member (variable). 

class Employee{
 private int empId;
 private String empName;
 private int empAge;
 public void setempId(int empId){
  this.empId = empId;
 }
 public int getempId(){
  return empId;
 }
 public void setempName(String empName){
  this.empName = empName;
 }
 public String getempName(){
  return empName;
 }
 public void setempAge(int empAge){
  this.empAge = empAge;
 }
 public int getempAge(){
  return empAge;
 }
}
 class EncapsulateExample{
 public static void main(String args[]){
  Employee emp = new Employee();
  emp.setempId(101);
  emp.setempName("Prashant");
  emp.setempAge(21);
  System.out.println("Employee Id is: " +emp.getempId());
  System.out.println("Employee name is: " +emp.getempName());
  System.out.println("Employee age is: " +emp.getempAge());
  
 }
}

Output:


You may also like these related posts:


Encapsulation in Java OOPs with Example Encapsulation in Java OOPs with Example Reviewed by Prashant Srivastava on December 28, 2018 Rating: 5

1 comment:

Powered by Blogger.