CS100J, Spring 2001
Tues 3/6
Lecture 13
-------------------------------------------------------------------------------
Announcements:
+ E5 due now
+ P4 due Tues 3/13
+ T2 on Thurs 3/15
-------------------------------------------------------------------------------
Summary:
+ algorithms: nouns and verbs -> (objects & variables) and methods
+ variables model state; methods model behavior
+ make objects when nouns are aggregate/compound things
  (called HAS-A RELATIONSHIPS)
+ treat classes as types in formal parameters and variable declarations
+ constructors can have multiple arguments
-------------------------------------------------------------------------------
Topics:
+ client-server relationship
+ encapsulation ($public$, $private$, $static$)
+ $this$
-------------------------------------------------------------------------------
Client-server relationship:
+ imagine objects interacting with eachother
+ need to share and use information

    +-----------+
    |  bank     | --->  A transacts
    | (savings) | <---
    +-----------+
        ^|      , \
        ||       \ \
        |V        \ \,
                   \  get
        B          set
   transacts

+ need something to prevent chaos in the savings account!
+ savings resembles instance variable
-------------------------------------------------------------------------------
Encapsulation:
+ also think of as INFORMATION HIDING
+ prevent clients from seeing/changing variables and some methods
  using the dot (.) operator
+ need to make black boxes and move things "away" from clients
+ choice of labeling fields as $public$, blank, $protected$, $private$
  (skip $protected$ for now)
+ $static$ related, too
+ effects on software development:
  - security
  - reliability of code
  - ease of reuse
+ abstraction:
  "worry" about higher levels of reasoning and not "nitty-gritty" details
-------------------------------------------------------------------------------
$private$
+ prevents another class from using a the "." operator to call
  a var or method
+ provides protection
+ how to modify classes? (style)
    - instance variables
    - UTILITY METHODS: methods used only in class (also called SUPPORT METHODS)
-------------------------------------------------------------------------------
$public$
+ blank (DEFAULT VISIBILITY) almost the same (actually a bit more restrictive)
  - information that is not "dangerous" to change
  - typically $static$, but not always
  - setters and getters: 
    methods that set and get info from instance variables and utility methods
  - called SERVICE METHODS
-------------------------------------------------------------------------------
Accessing:
+ from WITHIN the same object, all members (inst vars and meths) can "see" 
  eachother
+ from another object of the same class, you may use dot operator to access
  any member even if it's $private$
+ from another class (or object from another class):
  - anything labeled as $private$ cannot be accessed with the dot operator
  - use setter and getter methods to communicate with the object
  - also, use constructors to set initial information
    (constructors are typically $public$, though they can be $private$)
-------------------------------------------------------------------------------
$static$:
+ makes information accessible without instantiating an object
+ terminology:
  static variable: class variable
  static method:   class method
+ use the class name to access and/or change the field
+ if the field is $private$, then only objects of the same class can use
  the $static$ field (that's why $static$ fields tend to also be $public$)
+ once a static variable is changed, the new value is shared by all objects
  that are instantiated from the class
+ an object can be used to change a $static$ variable!
+ typically $static$ variables are also $final$ to prevent chaotic changes
  (but $final$ and $static$ are completely different: $final$ prevents a
  variable value from changing)
+ see Math class for examples
+ why is $main$ modified as $static$?
-------------------------------------------------------------------------------
method/constructor overloading:
+ use same method name with different # of params, param types, order of params
+ overloading does not consist of:
  - renaming formal params but keeping everything else the same
  - changing the return type
+ constructors can be overloaded as well
-------------------------------------------------------------------------------
$this$:
+ refers to current object, kind of like a reference variable

  class Person {
     Person friend;
     Person() {
         friend = new Person(); // friend gets a Person object
	 friend.friend = this;  // that object's friend is the current object!
     }
  }

+ use $this.varname$ to refer to the current object's varname inside a 
  block that contains the same varname
 
  class Blah {
     int x = 2; // this version of $x$ is shared by the entire current object
     void something() {
        int x = 1; // the $x$ is local to the scope of $something()$
        System.out.print(x+this.x); // outputs 3 when called
     }
  }

+ as a constructor call: calls the matching constructor of the current object
  
  class Thing {
     int x, y, z;

     Thing(int x) {
       this(x,x,x); //
     }

     Thing(int x, int y, int z) {
        this.x=x; this.y=y; this.z=z;
     }
  }

-------------------------------------------------------------------------------