Published on


Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part ofmentoring program. This is not official document of baabtra – MentoringPartnerbaabtra – Mentoring Partner is the mentoring division of baabte System Technologies Pvt. Ltd
  2. 2. ENCAPSULATION FREDDY P.V f4freddy@gmail.com
  3. 3. OOP
  4. 4. EncapsulationEncapsulationdescribes the ability of an objecthide its data and methods from therest of the world - one of thefundamental principles of OOP(Object Oriented Programming).
  5. 5. 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
  6. 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
  7. 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
  8. 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
  9. 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; } }
  10. 10. /* 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()); }}
  11. 11. 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.
  12. 12. Contact Us