The Usual Approach




     Correct functioning of classes depend on a lot of invariants.
     Programmers may not be aware of all of them.
     So let’s prevent programmers from accessing private metods
     and fields.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 10: Encapsulation   December 1, 2010   2/7
But. . .




      What if you really need to?
      What if you are willing to risk failure?
      After all, you may only hurt yourself. . .




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 10: Encapsulation   December 1, 2010   3/7
The Other Option




     Encapsulation is voluntary
     Violations are reported as a warning, not an error
     Programmers are educated about possible risks




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 10: Encapsulation   December 1, 2010   4/7
The Other Option




     Encapsulation is voluntary
     Violations are reported as a warning, not an error
     Programmers are educated about possible risks
     Encapsulation = ”not having to care what is inside” instead of
     ”not having access to what is inside”.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 10: Encapsulation   December 1, 2010   4/7
Representation Exposure




     Pointers pointing to subobject may manipulate them without parent’s
     object knowledge
     This may violate important invariants
     However, it is very difficult to avoid




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 10: Encapsulation   December 1, 2010   5/7
Possible Solutions




      Immutable objects
      Various sorts of const modifiers
      Copying




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 10: Encapsulation   December 1, 2010   6/7
Possible Solutions




      Immutable objects
      Various sorts of const modifiers
      Copying
      None of these solutions is satisfactory enough




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 10: Encapsulation   December 1, 2010   6/7
See




Boyland. Why we should not add readonly to Java (yet). Journal of
Object Technology (2006)




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 10: Encapsulation   December 1, 2010   7/7

Encapsulation

  • 2.
    The Usual Approach Correct functioning of classes depend on a lot of invariants. Programmers may not be aware of all of them. So let’s prevent programmers from accessing private metods and fields. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 10: Encapsulation December 1, 2010 2/7
  • 3.
    But. . . What if you really need to? What if you are willing to risk failure? After all, you may only hurt yourself. . . Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 10: Encapsulation December 1, 2010 3/7
  • 4.
    The Other Option Encapsulation is voluntary Violations are reported as a warning, not an error Programmers are educated about possible risks Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 10: Encapsulation December 1, 2010 4/7
  • 5.
    The Other Option Encapsulation is voluntary Violations are reported as a warning, not an error Programmers are educated about possible risks Encapsulation = ”not having to care what is inside” instead of ”not having access to what is inside”. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 10: Encapsulation December 1, 2010 4/7
  • 6.
    Representation Exposure Pointers pointing to subobject may manipulate them without parent’s object knowledge This may violate important invariants However, it is very difficult to avoid Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 10: Encapsulation December 1, 2010 5/7
  • 7.
    Possible Solutions Immutable objects Various sorts of const modifiers Copying Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 10: Encapsulation December 1, 2010 6/7
  • 8.
    Possible Solutions Immutable objects Various sorts of const modifiers Copying None of these solutions is satisfactory enough Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 10: Encapsulation December 1, 2010 6/7
  • 9.
    See Boyland. Why weshould not add readonly to Java (yet). Journal of Object Technology (2006) Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 10: Encapsulation December 1, 2010 7/7