Java Encapsulation: Definition and Examples

Rumman Ansari   Software Engineer   2024-07-04 03:58:19   8169  Share
Subject Syllabus DetailsSubject Details Login to Open Video
☰ TContent
☰Fullscreen

Table of Content:



All object-oriented programming languages provide several mechanisms that help you implement the object-oriented model. Encapsulation is one of the most fundamental OOP mechanisms.


Encapsulation in java

Encapsulation in Java is a mechanism of wrapping/binds the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.

Benefits of Encapsulation

  • Data Hiding: The user will have no idea about the inner implementation of the class. It will not be visible to the user that how the class is storing values in the variables. He only knows that we are passing the values to a setter method and variables are getting initialized with that value.

  • Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.

  • Testing code is easy: Encapsulated code is easy to test for unit testing.

  • Increased Flexibility: 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.

  • By providing only setter or getter method, The fields of a class can be made read-only or write-only.

  • A class can have total control over what is stored in its fields.


How to achieve encapsulation in Java ?

  • We can create a fully encapsulated class in Java by making all the data members of the class private.

  • Provide public setter and getter methods to modify and view the variables values.

Program:

//save as Student.java  
package pck1;  
public class Student{  
	private String name;  
   
public String getName(){  
	return name;  
}  
public void setName(String name){  
	this.name=name  
	}  
}  

 
//save as Encapsul.java  
package pck1;  
class Encapsul{  
public static void main(String[] args){  
	Student s=new Student();  
	s.setName("Rambo");  
	System.out.println(s.getName());  
	}  
}   

Compile: javac -d . Encapsul.java
Run: java pck1.Encapsul

Output:

Rambo

Real world Programming

Program:

/* File name : Student.java */
public class Student {
   private String name;
   private String id;
   private int age;

   public int getAge() {
      return age;
   }

   public String getName() {
      return name;
   }

   public String getId() {
      return id;
   }

   public void setAge( int newAge) {
      age = newAge;
   }

   public void setName(String newName) {
      name = newName;
   }

   public void setIdNum( String newId) {
      id = newId;
   }
}
/* File name : Encapsul.java */
public class Encapsul {

   public static void main(String args[]) {
      Student su = new Student();
      su.setName("Rambo");
      su.setAge(20);
      su.setId("21CSE");

      System.out.print("Name : " + su.getName() + " Age : " + su.getAge());
   }
}

Output:

Name : Rambo Age : 20

The public set() and get() methods are the access points of the instance variables of the Student class. 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.

MCQ Available

There are 1 MCQs available for this topic.

1 MCQ