Lecture15 - Classes, Encapsulation, Methods and...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and parameter passing Constructors Javadoc Reading for this lecture: L&L, 4.1-4.4, App I
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Accessors and Mutators A class usually provides methods to indirectly access and modify the private data values An accessor method returns the current value of a variable A mutator method changes the value of a variable The names of accessor and mutator methods take the form getX and setX , respectively, where X is the name of the value They are sometimes called “getters” and “setters”
Background image of page 2
3 Mutator Restrictions The use of mutators gives the class designer the ability to restrict a client’s options to modify an object’s state A mutator is often designed so that the values of variables can be set only within particular limits For example, the setFaceValue mutator of the Die class should restrict the value to the valid range (1 to MAX )
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Method Declarations A method declaration specifies the code that will be executed when the method is invoked (called) When a method is invoked, the flow of control jumps to the method and executes its code When complete, the flow returns to the place where the method was called and continues The invocation may or may not return a value, depending on how the method is defined
Background image of page 4
5 myMethod(); myMethod compute Method Control Flow If the called method is in the same class, only the method name is needed
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2010 for the course CS 110 taught by Professor Wilson during the Spring '10 term at University of Massachusetts Boston.

Page1 / 21

Lecture15 - Classes, Encapsulation, Methods and...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online