Disclaimer: This presentation is prepared by trainees of baabtra as a part of
mentoring program. This is not official document of baabtra – Mentoring
Partner
baabtra – Mentoring Partner is the mentoring division of baabte System Technologies Pvt. Ltd
ENCAPSULATION
                 FREDDY P.V
        f4freddy@gmail.com
OOP
Encapsulation


Encapsulation
describes the ability of an object
hide its data and methods from the
rest of the world - one of the
fundamental principles of OOP
(Object Oriented Programming).
Data hiding and encapsulation
    • Data-hiding or encapsulation is an important
      part of the OO paradigm.
    • Classes should carefully control access to their
      data and methods in order to
      – Hide the irrelevant implementation-level details so
        they can be easily changed
      – Protect the class against accidental or malicious
        damage.
      – Keep the externally visible class simple and easy to
        document
    • Java has a simple access control mechanism to
      help with encapsulation
      – Modifiers: public, protected, private, and package
        (default)
6
Derived class can restrict visibility
• Private
   – Protected and public members of base class are private in derived
     class.
• Protected
   – Protected and public members of base class are protected in derived
     class.
• Public
   – Protected and public members of base class are protected and public
     in derived class.
• Private members of base class aren’t visible in derived class.



                                                                         7
Access control
    • Access to packages
       – Java offers no control mechanisms for packages.
       – If you can find and read the package you can access it
    • Access to classes
       – All top level classes in package P are accessible
         anywhere in P
       – All public top-level classes in P are accessible anywhere
    • Access to class members (in class C in package P)
       –   Public: accessible anywhere C is accessible
       –   Protected: accessible in P and to any of C’s subclasses
       –   Private: only accessible within class C
       –   Package: only accessible in P (the default)


8
Getters and setters
    • A getter is a method that extracts information from an instance.
        – One benefit: you can include additional computation in a getter.
    • A setter is a method that inserts information into an instance (also
      known as mutators).
        – A setter method can check the validity of the new value (e.g., between 1
          and 7) or trigger a side effect (e.g., update a display)
    • Getters and setters can be used even without underlying matching
      variables
    • Considered good OO practice
    • Essential to javabeans
    • Convention: for variable fooBar of type fbtype, define
        – getBaabtraBar()
        – setBaabraBar(type x)



9
/* File name : baabtraTest.java
    */
public class baabtraTest{
private String name;
 private String idNum;            public String getIdNum(){
                                   return idNum;
 private int age;                  }
                                   public void setAge( int newAge){
 public int getAge(){              age = newAge;
                                  }
return age;
                                   public void setName(String
 }                                newName){
public String getName(){          name = newName;
return name;                       }
                                  public void setIdNum( String newId){ I
 }
                                  dNum = newId;
                                   }
                                   }
/* File name : RunEncap.java */
 public class RunEncap{

    public static void main(String args[]){
         baabtraTest encap = new baabtraTest();
        encap.setName("James");
        encap.setAge(20);
        encap.setIdNum("12343ms");
        System.out.print("Name : " + encap.getName()+ " Age :
        "+ encap.getAge());
    }
}
Benefits of Encapsulation:

• The fields of a class can be made read-only or
  write-only.
• A class can have total control over what is
  stored in its fields.
• The users of a class do not know how the class
  stores its data. A class can change the data
  type of a field, and users of the class do not
  need to change any of their code.
Contact Us

Encapsulation

  • 2.
    Disclaimer: This presentationis prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra – Mentoring Partner baabtra – Mentoring Partner is the mentoring division of baabte System Technologies Pvt. Ltd
  • 3.
    ENCAPSULATION FREDDY P.V f4freddy@gmail.com
  • 4.
  • 5.
    Encapsulation Encapsulation describes the abilityof an object hide its data and methods from the rest of the world - one of the fundamental principles of OOP (Object Oriented Programming).
  • 6.
    Data hiding andencapsulation • Data-hiding or encapsulation is an important part of the OO paradigm. • Classes should carefully control access to their data and methods in order to – Hide the irrelevant implementation-level details so they can be easily changed – Protect the class against accidental or malicious damage. – Keep the externally visible class simple and easy to document • Java has a simple access control mechanism to help with encapsulation – Modifiers: public, protected, private, and package (default) 6
  • 7.
    Derived class canrestrict visibility • Private – Protected and public members of base class are private in derived class. • Protected – Protected and public members of base class are protected in derived class. • Public – Protected and public members of base class are protected and public in derived class. • Private members of base class aren’t visible in derived class. 7
  • 8.
    Access control • Access to packages – Java offers no control mechanisms for packages. – If you can find and read the package you can access it • Access to classes – All top level classes in package P are accessible anywhere in P – All public top-level classes in P are accessible anywhere • Access to class members (in class C in package P) – Public: accessible anywhere C is accessible – Protected: accessible in P and to any of C’s subclasses – Private: only accessible within class C – Package: only accessible in P (the default) 8
  • 9.
    Getters and setters • A getter is a method that extracts information from an instance. – One benefit: you can include additional computation in a getter. • A setter is a method that inserts information into an instance (also known as mutators). – A setter method can check the validity of the new value (e.g., between 1 and 7) or trigger a side effect (e.g., update a display) • Getters and setters can be used even without underlying matching variables • Considered good OO practice • Essential to javabeans • Convention: for variable fooBar of type fbtype, define – getBaabtraBar() – setBaabraBar(type x) 9
  • 10.
    /* File name: baabtraTest.java */ public class baabtraTest{ private String name; private String idNum; public String getIdNum(){ return idNum; private int age; } public void setAge( int newAge){ public int getAge(){ age = newAge; } return age; public void setName(String } newName){ public String getName(){ name = newName; return name; } public void setIdNum( String newId){ I } dNum = newId; } }
  • 11.
    /* File name: RunEncap.java */ public class RunEncap{ public static void main(String args[]){ baabtraTest encap = new baabtraTest(); encap.setName("James"); encap.setAge(20); encap.setIdNum("12343ms"); System.out.print("Name : " + encap.getName()+ " Age : "+ encap.getAge()); } }
  • 12.
    Benefits of Encapsulation: •The fields of a class can be made read-only or write-only. • A class can have total control over what is stored in its fields. • The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.
  • 13.