This post is about the OCAJP exam objective “Apply encapsulation principles to a class”. In exam, You will asked like “How to make this class strongly encapsulated?”. In this article, we would explain encapsulation concept according to OCAJP exam and answer all your queries related to the encapsulation concepts.
What is Encapsulation
- Encapsulation is a process of binding the data (variables) and code acting on the data (methods) together as a single unit.
- Consider the example When you login into your Gmail or Yahoo mail account, there will be lot of back end process on which you have no control over it.
- So your password may be retrieved in an encrypted form, verified and only then you are given access. You do not have any control over how the password is verified and this keeps it safe from misuse.
Why we need Encapsulation
Let us consider the example to understand encapsulation purpose.
Example :
class Student { int id; String name; } public class Change { public static void main(String[] ar) { Student s = new Student(); s.id = -1; s.name=""; s.name=null; } }
- Look at Student class. It contains two instance variables with default as access modifier.
- So any class within same package of that class can assign and change values to those variables by creating object for that class.
- By this , We don’t have control over What values are going to store in Student class variables.
- We can solve this problem by applying encapsulation to the Student class.
How to implement Encapsulation
- The Java Bean class is the example of fully encapsulated class.
- The below are the rules to achieve encapsulation in Java
- Declare the variables of a class as private.
-
Example :
private int id; private String name;
- Provide public setter methods to assign or change the variables values. The setter method name must have a prefix of “set” followed by the first letter of the property in uppercase followed by the remaining variable name.It should have “public” access modifier and “void” return type.
Example :
public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; }
- Provide public getter methods to get the variables values. The getter method name must have a prefix of “get”(if variable is not boolean) or “is”(if variable is boolean) followed by the first letter of the property in uppercase followed by the remaining variable name.It should have “public” access modifier and type of that variable as return type.
Example :
public int getId() { return id; } public String getName() { return name; } public boolean isAllowed(){ return true; }
- Below is the complete program.
Example :
class Student { private int id; private String name; public int getId() { return id; } public String getName() { return name; } public void setName(String name) { if (!name.equals("") && !name.equals(null)) this.name = name; } public void setId(int id) { if (id > 0) this.id = id; } } public class Change { public static void main(String[] ar) { Student s = new Student(); s.setId(1); s.setName("OCA"); System.out.println(s.getId() + " " + s.getName()); } }
Encapsulation benefits
- A class can have total control over what is stored in its fields.
- The fields of a class can be made read-only or write-only.
Conclusion
We hope this article has provided good amount of details about the encapsulation and why it is necessary in the Java language. This objective will be tested in the OCAJP exam by asking the various rules that are either true or false. For the OCAJP exam remember Java Bean class rules and naming conventions.
If you have any questions on OCAJP exam, please contact our support for help.
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021