Blog Java OCAJP – HOW TO Apply encapsulation principles to a class?

OCAJP – HOW TO Apply encapsulation principles to a class?

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.

OCAJP Encapsulation

What is Encapsulation

Image result for java 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.

what is encapsulation

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;
      
      1. 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;
      	}
      
      1. 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.

    About Aditi Malhotra

    Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here