Presentation is loading. Please wait.

Presentation is loading. Please wait.

CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.

Similar presentations


Presentation on theme: "CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that."— Presentation transcript:

1 CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that object.  Instance Fields ( Data)  Methods ( Functions that manipulate data) Classes should represent a single concept (entity). Classes should represent a single concept (entity). When designing Classes: Start by identifying Objects and the classes to which they belong. When designing Classes: Start by identifying Objects and the classes to which they belong. Classes (Objects) are not actions, they are entities. Classes (Objects) are not actions, they are entities.  DON’T turn and “action” into a class! General Rule of Thumb: General Rule of Thumb:  Class Names -> use NOUNS (person, place, or thing).  Methods( ) ->use VERBS (action – do something).

2 Method Concepts(Review): Methods can be either public or private depending where access is granted. Methods can be either public or private depending where access is granted. Methods with a void return type are invoked as a stand alone line of code. Methods with a void return type are invoked as a stand alone line of code.  acct.dep(500); Methods with a non-void return type are invoked through an assignment or imbedded within existing code: Methods with a non-void return type are invoked through an assignment or imbedded within existing code:  System.out.println(acct.getBal( )); Methods invoked within the class defining them DON’T need to be invoked with dot notation. Methods invoked within the class defining them DON’T need to be invoked with dot notation.  header( )  x = getNum( ); Methods need to be invoked by a class object variable through dot notation when access is from outside the invoking objects class. Methods need to be invoked by a class object variable through dot notation when access is from outside the invoking objects class.  weeklyBudget = acct.withdraw(100); Methods should perform one basic task. Methods should perform one basic task.

3 Method Concepts – New Terminology Accessor & Mutator Methods: Accessor & Mutator Methods:  An accessor method does not change the state of its implicit parameter (acct.getBal( ) ).  A mutator method can change the state ( acct.dep(100)). Immutable Class : class with no mutator methods. Immutable Class : class with no mutator methods.  String class is immutable. None of its methods can actually alter the original string.  An immutable class is very safe because you cannot modify the state of the implicit parameter.  Immutable classes do not allow for side effects. Parameter Passing: Java passes ALL parameters to a method by value. Passing paramters is like an assignment statement, assigning to the formal (explicit) parameter a copy of the value stored in the actual parameter. Therefore: Parameter Passing: Java passes ALL parameters to a method by value. Passing paramters is like an assignment statement, assigning to the formal (explicit) parameter a copy of the value stored in the actual parameter. Therefore:  Primitive explicit parameters cannot be changed! The copy is changed in the method, but the values at the invoking level are not!  When an object is passed to a method, a copy of the address is made. So we in effect have two references aliasing the same location. So object parameters have the ability to change.  Changing an objects state through parameter passing is referred as a side effect, and it should be avoided at all costs!


Download ppt "CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that."

Similar presentations


Ads by Google