Java Encapsulation (Part 2)
Java Encapsulation
Encapsulation is making sure that sensitive information is hidden from the user , only required information is provided / displayed.
It can be understood by comparing encapsulation with a capsule.
We can consider a private class which can be compared to a coating of a capsule. It is the class that contains all the sensitive information in it. Methods and variables can be compared to all the medicine inside the capsule i.e the main data.
To achieve encapsulation we must declare variables / attributes of the class as private.
Advantages of Encapsulation
- Better control of class attributes and methods
- Class attributes can be made read-only (if you only use the
getmethod), or write-only (if you only use thesetmethod) - Flexible: the programmer can change one part of the code without affecting other parts
- Increased security of data.
- The encapsulated class is easy to test. So, it is better for unit testing.
Getters & Setters
You learned from the previous chapter that private variables can only be accessed within the same class (an outside class has no access to it). However, it is possible to access them if we provide public get and set methods.
The get method returns the variable value, and the set method sets the value.
eg:-
classPerson{privateStringname;// providing get methodpublic StringgetName() { returnname; }// providing set methodpublic voidsetName(Stringname) {//here this.name refer to current objectthis.name=name; } } public classMyClass{ public static void main(String[] args) { Personperson1= new Person();person1.setName("Bob"); System.out.println(person1.getName()); } }
Output:-
Bob
The get method returns the value of the variable name.
The set method takes a parameter (newName) and assigns it to the name variable. The this keyword is used to refer to the current object.
However, as the name variable is declared as private, we cannot access it from outside this class:
Let us consider another program of a bank account , where user inputs name and bank account number .
Here I am declaring all attributes as private and its bank account number as " * ".
import java.util.Scanner; classBankInfo{privateStringname;privateStringbankAccNo;//generating getters and setterspublic StringgetName() { returnname; } public voidsetName(Stringname) { this.name=name; } public intgetBankAccNo() { intlength=bankAccNo.length();// getting length of stringreturnlength; } public voidsetBankAccNo(Stringstr) { this.bankAccNo=str; } } public classMyClass{ public static void main(String[] args) { Scannerscan= new Scanner(System.in); BankInfoperson1= new BankInfo(); System.out.println("Welcome to National Bank"); System.out.println(" "); System.out.println("Please enter your name:");person1.setName(scan.nextLine());
System.out.println("Please enter your Bank Acc No:");person1.setBankAccNo(scan.nextLine());scan.close(); System.out.println(" "); System.out.println("Thank You for Your Response:"); System.out.println(" "); System.out.println("Your Name is:" +person1.getName()); System.out.println("For Privacy Concern your Bank Acc No is:");//for printing '*' in place of bankAccNofor (inti= 0;i<person1.getBankAccNo();i++) { System.out.print("*"); } } }
Output:-
Welcome to National Bank
Please enter your name:
Java Monk
Please enter your Bank Acc No:
123abc456abc
Thank You for Your Response:
Your Name is: Java Monk
For Privacy Concern your Bank Acc No is:
************ 
