ENCAPSULATION

Encapsulation


  • Wrapping similar data into single unit is called as Encapsulation.
  •              ( OR )
  • Binding properties and methods into a single block is called as Encapsulation.

  • The best example of Encapsulation is a class definition of the class.

    Encapsulation are of 2 types. They are
      1.) Strong Encapsulation
      2.) Weak Encapsulation

1. Strong Encapsulation :

  • A class that contains all property access levels as private & every method as a public. Then that satisfies the Strong Encapsulation and then that class is called as Strong Encapsulation class.
  • Through Strong Encapsulation we can’t access any properties directly outside from the class.
  • If want to access or initialize the value to the properties. Then we should run through public method.

Example 1:

     
    						
                        package com.sdj;
                        class Person
                        {
                           private int age;
                           private String name;
                           private String address;
                           public int getAge()
                           {
                              return age; 
                           }
                           public void setAge(int age)  
                           {
                              this.age = age;
                           }
                           public String getName()
                           {
                              return name;
                           }
                           public void setName(String name) 
                           {
                              this.name = name;
                           }
                           public String getAddress()  
                           {
                              return address;
                           }
                           public void setAddress(String address) 
                           {
                              this.address = address;
                           }   
                        }
                        public class Manager
                        {
                           public static void main(String[] args) 
                           {
                              Person p=new Person();
                              p.setAge(23);
                              p.setName("Praveen");
                              p.setAddress("Jaipur");
                              System.out.println(p.getName());
                              System.out.println(p.getAge());
                              System.out.println(p.getAddress());
                           }
                        } 
                            

       Output:

      Praveen 23 Jaipur

    • Person class is 100% Strong Encapsulation class. Why because inside Person class every attribute access level is private and method access level is public.
    • If any class contain setters & getters for each property it is called as JavaBeans.


Rules for Setters & Getters :

    Setters :

    • Setters method should start with set followed by property name. In property name first letter should be capital letter.
    • set() will take one parameter. The parameter type should be same as the corresponding attribute return type.
    • set() should not return any value because it’s return type is void.

    Getters :

    • getters method should start with get followed by property name. In property name first letter should be capital letter.
    • get() won’t take any parameter.
    • get() should contain the return type. It is same as the corresponding attribute datatype and it’s access level should be public.

2. Weak Encapsulation :

  • If class contain any property access level other than private and method access level other than public. Then that class is called as Weak Encapsulated class.

Example 2:

     
    						
                        package com.sdj;
                        class Person
                        {
                           int age;
                           public String name;
                           private String address;
                           public int getAge()
                           {
                              return age;
                           }
                           public void setAge(int age) 
                           {
                              this.age = age; 
                           }
                           String getName() 
                           {
                              return name;
                           }
                           public void setName(String name)
                           {
                              this.name = name;  
                           }
                           public String getAddress() 
                           {
                              return address;
                           }
                           public void setAddress(String address)
                           {
                              this.address = address; 
                           }
                        } 
                            
    • The above class breaks the Strong Encapsulation why because the property age access level is default & name access level is public.
    • The getName() access level is default. So the above class is Weak Encapsulation.
    • Note
      • The best application developer follows the Strong Encapsulation & loose coupling.