One object the client can access and modify another

This preview shows page 8 - 13 out of 25 pages.

One object (the client) can access and modify another object’s state through it’s methods. Example: dieObj.setFaceValue(6); We should make it difficult, if not impossible, for a client to access an object’s variables directly dieObj.faceValue = 6; The second line should cause a compile-time error; otherwise, the class violates encapsulation
Image of page 8

Subscribe to view the full document.

9 COMP 1210 – Fundamentals of Computing I Slide 4 - 17 Encapsulation An encapsulated object can be thought of as a black box -- its inner workings are hidden from the client The client invokes the interface methods of the object, which manages the instance data Data Methods Client COMP 1210 – Fundamentals of Computing I Slide 4 - 18 Visibility ( Access ) Modifiers How do we make sure not to violate encapsulation? visibility modifiers define who can access an instance variable or a method Java has three visibility modifiers: public , protected , and private • The protected modifier involves inheritance, which we will discuss later The fields, constructors, and methods of a classes (collectively called members of the class ) can each have an access modifier
Image of page 9