Let’s learn encapsulation in java.
Encapsulation in java
Encapsulation is a procedure of encapsulating combining data members and methods into a single unit.
Encapsulation is achieved by declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.
In encapsulation data of a class are hidden from any another class and can be accessed only through methods or functions of its own class in which the class is declared.
Encapsulation is one of the four fundamental Object Oriented Programming(OOPS) concept. Other three oops concept are polymorphism, inheritance and abstraction.
Let’s see an example on encapsulation in java.
class EncapsulationExample { private int ID; private String stuName; private int stuAge; // getter and setter methods public int getStudentID() { return ID; } public String getStudentName() { return stuName; } public int getStudentAge() { return stuAge; } public void setStudentAge(int number) { stuAge = number; } public void setStudentName(String number) { stuName = number; } public void setStudentID(int number) { ID = number; } } public class ExampleForEncapsulation { public static void main(String[] args) { EncapsulationExample student = new EncapsulationExample(); student.setStudentName("Virat"); student.setStudentAge(5); student.setStudentID(2353); System.out.println("Student Name: " + student.getStudentName()); System.out.println("Student ID: " + student.getStudentID()); System.out.println("Student Age: " + student.getStudentAge()); } }
Output:
Student Name: Virat
Student ID: 2353
Student Age: 5
In the above example variables ID, stuName and stuAge will be hidden from other classes.
These variables are accessed only through setter and getter methods of the current class. This theory is known as data hiding. Now let’s see real time example on encapsulation example in java.
class ABCBank { // private data members private long accountNumber; private String name, emailID; private float money; // getter and setter methods public long getAccountNumber() { return accountNumber; } public void setAccountNumber(long accountNumber) { this.accountNumber = accountNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmailID() { return emailID; } public void setEmailID(String emailID) { this.emailID = emailID; } public float getMoney() { return money; } public void setMoney(float money) { this.money = money; } } public class RealWorldEncapsulationExample { public static void main(String[] args) { ABCBank obj = new ABCBank(); // setting values through setter methods obj.setAccountNumber(1112316548); obj.setName("Virat"); obj.setEmailID("virat@gmail.com"); obj.setMoney(2300000f); // getting values through getter methods System.out.println("Account Number: " + obj.getAccountNumber()); System.out.println("Name: " + obj.getName()); System.out.println("Email ID: " + obj.getEmailID()); System.out.println("Money: " + obj.getMoney()); } }
Output:
Account Number: 1112316548
Name: Virat
Email ID: virat@gmail.com
Money: 2300000.0
Advantages of encapsulation in java
- Data hiding: by hiding the variables user will have no idea about inner implementation of the class.
- Flexibility: Here we can make variables of a class as read only or write only by omitting setter method or getter method.
- Reusability: we can reuse the code depending on the requirement. Also validating values before modifying.
- Security and maintainability.
- Enhancement will be easy.
Why encapsulation is used?
By using encapsulation we are able to protect the members of a class from external access in order to gaurd aganist unauthorized access. That is we are stopping other classes outside of the class that a user is working on, from accessing the inner workings of a class.
By making fields private, we are making sure that the fields within the class aren’t accessible to any other classes.
As a java programmer it is important to keep things confined and sort of reduce access or protect.
Also read – major features of java