This post will discuss the OCAJP exam objective, “Apply encapsulation principle to a class”. Examiners will ask you how to encapsulate this class. This article will explain the OCAJP exam encapsulation concept and answer any questions you may have about encapsulation concepts.
OCAJP 8 Practice Questions
What is Encapsulation?
Encapsulation refers to the process of binding the data and code that act on the data together as one unit.
Consider this example: When you log in to your Gmail or Yahoo Mail account, there are many back-end processes over which you have no control.
Your password can be recovered in encrypted form and verified before you are granted access. You have no control over the way the password is verified, which keeps it safe from misuse.
Why Encapsulation is Important
Let’s take a look at the example to understand the purpose of encapsulation.
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 that use default as an access modifier.
Any class in the same package can assign or change values to these variables by creating an object for that class.
This means that we don’t have any control over the values stored in Student class variables.
This problem can be solved by applying encapsulation in the Student class.
How to implement Encapsulation
The Java Bean class is an example of a fully encapsulated class.
These are the steps to achieve Java encapsulation
Declare variables of a class private.
Example:
Private int id;Private String name; Provide public setter methods to assign and change variables values. The setter method name should include the prefix “set” followed with the first letter of each property in uppercase.
Example:
public void setId (int id); this.id=id;public.void setName(String title) this.name=name;Provide public methods to obtain the variables’ values. The name of the getter method must include a prefix of “get” (if the variable is not boolean), or “is” (if the variable is boolean), followed by the first letter in uppercase, followed by the remaining property name.
Example:
public int getId() return id;public String getName() return name;public boolean isAllowed() return true;
Here 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 complete control over what is stored on its fields.
You can make the fields of a class read-only or write only.
Conclusion
600+ Practice Questions for OCAJP Certification Exam
We hope that this article has provided enough information about encapsulation and its importance in Java. This objective will be tested by the OCAJP exam, which will ask you to answer the questions that are true or false. Java Bean class rules, naming conventions, and Java Bean are important for the OCAJP exam.
For any questions regarding the OCAJP exam, please contact support.