Skip to content

Encapsulation In Java Programming Language

In this article, you will learn Encapsulation in Java Programming Language. In general terms, encapsulation means enclosing something in a capsule in order to protect it from being damaged by the outside means.

Similarly in Java, Encapsulation is the process of making the fields and methods enclosed in a class that cannot be accessed outside the class. They are only accessible by the methods of that particular class.

Also Read how to Modifiers Used In Java Programming Language

In java programming language, encapsulation can be achieved in the following two ways.

  1. adding private access modifier to the variables, and
  2. employing public setter and getter methods to a java program.

[1] ENCAPSULATING VARIABLES USING PRIVATE ACCESS MODIFIER

Let’s now first use the private access modifier in a variable and see if we can access them through methods in the other class.

Syntax: 

private datatype variablename ;

Example: 

class W3TrainingSchool {
private String trainername = “Amit Negi” ;
private void trainerProfile() {
System.out.println(“Digital Marketing Trainer”) ;
}
}

class W3TS {
public static void main(String args[]) {
W3TrainingSchool obj = new W3TrainingSchool( ) ;
System.out.println(obj.trainername ) ;
obj.trainerProfile( ) ;
}
}

Output: The following output will be generated after running the above java codes.

Main.java:17: error: trainername has private access in W3TrainingSchool
System.out.println(obj.trainername ) ;
^
Main.java:18: error: trainerProfile() has private access in W3TrainingSchool
obj.trainerProfile( ) ;
^
2 errors

Explanation:

Clearly, the above java codes failed to run. This is because the variable and method inside the class ‘W3TrainingSchool’ are declared private.

The other class ‘W3TS’ is trying to access that private declared variable and method which is not possible. This is one of the examples of achieving encapsulation in java programming language.

[2] USE OF PUBLIC SETTER AND GETTER METHODS

Using a private access modifier will hide the variables and limits their scope outside the class. This process is also called ‘data hiding’. In order to have control over these hidden variables, public setter and getter methods are used.

Each time a method in other class tries to access the private declared variables, it will have to call these setter and getter methods to take permission to set and view the value of those variables respectively. This provides security of the data.

Syntax:

  • public datatype setXXX() { }
  • public datatype getXXX() { }

Example:

class W3TrainingSchool {
private String trainername ;
private int salary ;

public String getTrainerName () {
return trainername ;
}

public int getSalary () {
return salary ;
}

public void setTrainerName(String newTrainerName) {
trainername = newTrainerName ;
}

public void setSalary(int newSalary) {
salary = newSalary ;
}
}

class W3TS {
public static void main(String args[]) {
W3TrainingSchool obj = new W3TrainingSchool ( ) ;
obj.setTrainerName(“Amit Negi”) ;
obj.setSalary(45000) ;
System.out.println(“Trainer’s name =” + obj.getTrainerName()) ;
System.out.println(“Trainer’s salary =” + obj.getSalary ()) ;
}
}

Output:

Trainer’s name = Amit Negi
Trainer’s salary = 45000

Also Read:- Abstraction in Java Programming Language

Explanation:

In this example, we have used public setter methods to modify(set) the values of variables ‘trainername’ and ‘salary’ to ‘Amit Negi’ and ‘45000’ respectively. We have also used public getter methods to view(get) those modified values.

Clearly, we can change or modify the values of private declared variables using public setter methods and view those modified values through public getter methods.

We have provided you the best possible description on “What is Encapsulation in Java Programming Language”. Hope you like this post. For more updates and related information, stay connected to our blogs on java programming language.

Facebook
Twitter
LinkedIn
Pinterest