Encapsulation in Java OOPs with Example - Javastudypoint

Friday, December 28, 2018

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 it hides the implementations details from users. A capsule is an example of encapsulation which is mixed of several medicines.
Encapsulation in Java OOPs

In Java, we can create a fully encapsulated class by declaring all the data member (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 getter and setter method.

package com.javastudypoint;
public class Employee
{
private int eid;                  // private member
private String name;

public void setName(String name)  // setter method
{
this.name = name;
}
public String getName()     // getter method
{
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). 

package com.javastudypoint;
public class Employee
{

// private data members.
private int empid;
private String empname;
private int empage;

// public setter and getter method.
public void setEmpId(int id)
{
this.id = id;
}
public int getEmpId()
{
return id;
}


public void setEmpName(String name)
{
this.name = name;
}
public int getEmpName()
{
return name;
}


public void setEmpAge(int age)
{
this.age = age;
}
public int getEmpAge()
{
return age;
}
}

// A java class that runs the Encapsulated class Employee.
public class EncapsulateEmployee
{
public static void main(String args[])
{

// An instance of Employee class is created.
Employee emp = new Employee(); 

// setting the values through setter method. 
emp.setEmpId(101);
emp.setEmpName("Prashant Srivastava");
emp.setEmpAge(20);

//getting the values through a getter method.
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:

Employee Id is: 101
Employee Name is: Prashant Srivastava
Employee Age is: 20


You may also like:









No comments:

Post a Comment