Lecture 26, CS 302-7 and 8, March 21

  1. Review:
    1. Object Oriented Programming
    2. Objects and Classes

                                                               i.     Classes – compile time

                                                             ii.     Objects – run time

                                                            iii.     Example – blueprint/house

    1. Encapsulation

                                                               i.     Public Interface

    1. Instance Methods

                                                               i.     Accessors

                                                             ii.     Mutators

    1. In class example

                                                               i.     Voting Machine

    1. Reminder:

                                                               i.     Method declarations - public interface

                                                             ii.     Method bodies - private implementation

  1. Instance variables
    1. We’ve been talking about instance methods up to this point
    2. Instance variables allow an object to store data

                                                               i.     Declared for the entire class, not just for one method

    1. Think of the house – it has properties like color, square footage, etc

                                                               i.     These are mentioned in the blue print (declared), but they vary for different instances of houses built from that blueprint (objects).

    1. How to think about instance variables – they store all of the data the object will ever need.

                                                               i.     Need to consider the needs of each method in determining how many/which ones to declare

    1. Think back to the voting machine example – we need to remember how many votes for the democrat and the republican.

                                                               i.     We'll have an instance variable to keep track of each of these.

    1. How to declare?

                                                               i.     Declared in the class, outside any methods.

                                                             ii.     Usually at the top of the class.

                                                            iii.     private <type> <name>;

    1. Each object stores its own copy of these variables/this data
    2. Definition – 'state' of the object – values stored in instance variables.
    3. Default value – 0, null, etc – until we initialize it
    4. Think back – what does a mutator do?  It changes the state of an object.  So, it changes an instance variables
    5. What does an accessor do?  It gets an instance variable (and maybe sends back a modified version of it)
    6. private vs public

                                                               i.     Since we declare these as private, they cannot be accessed directly by the object.  Must use accessor instead.

  1. Instance Methods
    1. Some review:

                                                               i.     Not static, usually public

                                                             ii.     Located after instance variables in our classes

    1. Parameters for these instance methods:

                                                               i.     Explicit parameter – declared in method header

1.    0 or more of them

                                                             ii.     Implicit parameter – the object itself

1.    One and only one

    1. this keyword

                                                               i.     Holds reference to the object itself (the implicit parameter)

                                                             ii.     Use to refer to instance variables from within instance methods

1.    this.<var>

    1. Example – how implement voteDemocrat?

                                                               i.     For now, we won't consider the undoVotes functionality

1.    this.demVotes++;

    1. Example – how implement getDemVotes?

return this.demVotes;

    1. Example – VotingMachine.java
  1. Constructors
    1. basic format:
    2. public <className>(<type>  <varName>,...){}
    3. Purpose – initialize instance variables
    4. Called when you new the object
    5. Basic form – just set everything to default values
    6. Think of Random – what happens when you pass in a seed?  That's stored in an instance variable
    7. So, that's an example of having params on these.