Search Logic Blocks

Sunday, June 21, 2020

Java: What is Encapsulation?

Encapsulation means the action of enclosing something in or restricting access to something. Java uses the principle of encapsulation - bundles (hides) the data and methods (behaviors) in a class definition. In my previous posts and examples, I have explained how we can use the variables and methods in the class together.



By putting related data and method altogether in an class, it becomes easy and safe way to handle data. Whenever we need to change a specific class, we have to change only one class, means we don't have to compile whole program. We need to compile only one class. And that we can restrict access to the class components from outside. In Java, to define the scope of the variables and methods, access modifiers are used. There are 3 access modifiers - public, private and protected.

Public is the default access modifier. It means, the class components (variables and methods) declared as public can be accessed by the any other classes outside from the class.

Class AccessMod1 (AccessMod1.java)
// To declare public access modifier class components
public class AccessMod1 {
public int num = 10;

public void printNum() {
System.out.println("Calling from the class itself: " + num);
}
}
Class AccessTest1 (AccessTest1.java)
// To test public access modifier class components
public class AccessTest1 {
public static void main(String args[]) {
AccessMod am = new AccessMod();

// It can access directly as the variable is public
System.out.println("Calling From Main Class: " + am.num);

// It can access directly to the method as it is public
am.printNum();
System.out.println();

// Public variables can be changed directly
am.num = 15;
System.out.println("Calling From Main Class: " + am.num);
am.printNum();
}
}
Output :

Calling From Main Class: 10
Calling from the class itself: 10

Calling From Main Class: 15
Calling from the class itself: 15

Private access modifier restricts access for the variables and methods to the class itself. No other class can access directly the variables and methods declared as private.

Class AccessMod2 (AccessMod2.java)
// To declare private access modifier class components
public class AccessMod2 {
private int num = 10;

public void printNum() {
System.out.println("Calling from the class itself: " + num);
showMsg();
}

public void changeNum() {
num = num + 5;
}

private void showMsg() {
if(num > 12) System.out.println("Number > 12");
}
}
Class AccessTest2 (AccessTest2.java)
// To test private access modifier class components
public class AccessTest2 {
public static void main(String args[]) {
AccessMod am = new AccessMod();

// It can not access directly as the variable is private
// System.out.println("Calling From Main Class: " + am.num);

// It can access directly to the method as it is public
am.printNum();
System.out.println();

// Private variables can not be changed directly
// am.num = 15;
// Private variables can only changed indirectly by the class methods
am.changeNum();

// This time private method is called internally, which can't be called directly
// am.showMsg();
am.printNum();
}
}
Output:

Calling from the class itself: 10

Calling from the class itself: 15
Number > 12

Protected access modifier restricts access for the variables and methods to its package classes (all classes in the package including non-subclasses) and its child (inherited) classes in other packages.

Note: I will explain Inheritance shortly.

No comments: