Loading Posts...

Java Encapsulation Tutorial With Example | Encapsulation in Java

Java Encapsulation Tutorial With Example

Java Encapsulation Tutorial With Example | Encapsulation in Java is today’s topic. Encapsulation is one of the four fundamental (inheritance, abstraction, and polymorphism) Object Oriented Programming concept. It a kind of mechanism of wrapping data in a single unit. In encapsulation variables of a class are kept hidden from other classes. We use access specifier to make these data private, protected (To make variable private in its child class). And this is known as Data Hiding.

Java Encapsulation Tutorial

Encapsulation in Java is the mechanism of wrapping a data (variables) and code acting on the data (methods) together as the single unit. In the encapsulation, the variables of a class will be hidden from the other classes and can be accessed only through the methods of their current class. See the below image.

Java Encapsulation Tutorial

 

Process of achieving Encapsulation

We will discuss here how to achieve encapsulations step by step.

Firstly, if you want to hide the data from another class, then we have to make those variables(data) as private.

Secondly, we need to create two public methods to access this data from outside of the class. These methods are generally known as getter and setter method.

Let’s take an example to understand this concept more vividly.

//creating a student class
class Student
{
	//decalring all variables as private to achieve encapsulation
	private int regNo,age;
	private String name;
	private double cgpa;
	
	//creating setter method to get registration number of a student
	public void setReg(int regNo)
	{
		this.regNo=regNo;
	}
	
	//it is a setter method which will return the reg_no of the student 
	public int getReg()
	{
		return regNo;
	}
	
	//setter method for name 
	public void setName(String name)
	{
		this.name = name;
	}
	
	//getter method for name
	
	public String getName()
	{
		return name;
	}
	
	//setter method for age
	public void setAge(int age)
	{
		this.age = age;
	}
	//getter method for age
	
	public int getAge()
	{
		return age;
	}
	//setter method for cgpa
	
	public void setCgpa(double cgpa)
	{
		this.cgpa = cgpa;
	}
	
	//getter method for cgpa
	
	public double getCgpa()
	{
		return cgpa;
	}
}
//driver class
public class Test
{
     public static void main(String []er)
	 {
		Student s = new Student();
		
		//using setter method to access those private variables 
		
		s.setReg(1701200);
		s.setName("Abhinandan Trivedi");
		s.setAge(20);
		s.setCgpa(7.8);
		
		//using getter method 
		
		System.out.println("Registration:"+s.getReg());
		System.out.println("Name:"+s.getName());
		System.out.println("Age:"+s.getAge());
		System.out.println("CGPA:"+s.getCgpa());
	 }
}

The output is following.

 

In that above example, We’ve declared all the variables of Student class as private and to access these variables we’ve created setter and getter methods for every variable. Normally, these methods are referred to as the getters and setters. That is why if any class that wants to access the variables should access them through these getter and setter methods.

Why we need encapsulation

The meaning of Encapsulation is to make sure that the “sensitive” data is hidden from the users. If we need to achieve this, you just need the following things.

  • Declare class variables/attributes as private (only accessible within the same class).
  • It provides public setter and getter methods to access and update the value of the private variable.

You can find also find other OOP concepts in this blog like Abstract class, Interface, and Constructor in Java. It has a very detail explanation about these concepts.

Advantages of Encapsulation

  • Encapsulation improves maintainability, re-usability, and flexibility.
  • In this case, the user would not be knowing about the fact when and how data is changing. The user only knows about updating data not knowing about get and set methods. In this way, we can secure our private details from the world. The code can be maintained at any point of time without breaking the classes that use the code. It improves the re-usability of the underlying class.

Conclusively, Java Encapsulation Tutorial With Example | Encapsulation in Java article is over.

The post Java Encapsulation Tutorial With Example | Encapsulation in Java appeared first on AppDividend.

user

The author didnt add any Information to his profile yet

Loading Posts...
WP2FB Auto Publish Powered By : XYZScripts.com