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
get
method), or write-only (if you only use theset
method) - 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:-
class
Person
{
private
String
name
;
// providing get method
public String
getName
() { return
name
; }
// providing set method
public void
setName
(String
name
) {
//here this.name refer to current object
this.
name
=
name
; } } public class
MyClass
{ public static void main(String[] args) { Person
person1
= 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; class
BankInfo
{
private
String
name
;
private
String
bankAccNo
;
//generating getters and setters
public String
getName
() { return
name
; } public void
setName
(String
name
) { this.
name
=
name
; } public int
getBankAccNo
() { int
length
=
bankAccNo
.length();
// getting length of string
return
length
; } public void
setBankAccNo
(String
str
) { this.
bankAccNo
=
str
; } } public class
MyClass
{ public static void main(String[] args) { Scanner
scan
= new Scanner(System.in); BankInfo
person1
= new BankInfo(); System.out.println("
Welcome to National Bank
"); System.out.println(" "); System.out.println("
P
lease 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 bankAccNo
for (int
i
= 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:
************