CS100J, Fall 2001 Thurs, 10/11 Lecture 12 ------------------------------------------------------------------------------- Announcements: + programming contest: http://www.cs.cornell.edu/kempe/contest/default.html + reading: still chapters 4 and 5 + T1 regrades due Mon 10/22 (see 10/3 Announcements) + P4 has been posted (due 10/18_ + E3 due Tues 10/16 + T2 on 10/23 + "cutoff grade" given to engineering doesn't mean what they think it means ------------------------------------------------------------------------------- Topics: + evals + summary + constructors (a bit more, more will be coming!) + references (a bit more) + scope + encapsulation ------------------------------------------------------------------------------- Evals: - website, examples in lecture, notes, types of projects ------------------------------------------------------------------------------- Summary: + OOP approach: translate problem into classes from which you create objects that interact + Writing classes: members, which fields and methods + Creating objects from classes: references are the addresses of the objects + Developing classes: "has-a" relationships and aggregation - think of classes as types - state and behavior (nouns and verbs) become members ------------------------------------------------------------------------------- A Bit More About References: (Section 4.3, pg 304) + references have a "zero" value called $null$ (keyword) + $null$ means "no object" or "an address of nowhere" see nurses.java ------------------------------------------------------------------------------- A Bit More About Constructors (Section 5.5) + a constructor is a special kind of method + returns the reference to a newly created object + does not have a return type written! + has the same name of a class + all classes must have at least one (can have > 1 --> will see later) + if you do not give one, Java AUTOMATICALLY uses classname() {} (this is called the empty or default constructor) + if you write a non-empty constructor, Java does NOT give you the default! + because constructors resemble methods, you can call them with values + WHY BOTHER? use constructors to: - set fields - call other methods during creation of object - provide more extravagant ways of creating an object other than the default ------------------------------------------------------------------- // Problem1: // which of the following are constructors? public class Thing { int x; Thing() { } Thing(int value) { x = value; } void test1(Thing value) { } Thing test2(int value) { return new Thing(value); } } ------------------------------------------------------------------- // Problem2: // fill in the blanks to write Interval class // an interval is a set that looks like [low,high] class Interval { double low; double high; Complex(double l, double h) { } public toString() { return "["+_____+","+______+"]"; } } // class Interval public class Test { public static void main(String[] args) { Interval i = new ________________________ ; System.out.println( _____ ); // print newly created interval } } // Class Test ------------------------------------------------------------------------------- Scope: + for identifiers: "the region of a program source within which it represents a certain thing" (see http://foldoc.doc.ic.ac.uk/foldoc/index.html) + DIS's definition: scope is level of visibility or "location of visibility" (whether or not a piece of code can access/use another) + Basic structure of a class: class { fields; methods(params) { vars; { blockvars; } } } + scoping rules for code WITHIN a class: - fields can access "previous" fields but not see "ahead" (fields are initialized top-down, left-to-right) - all methods see all the fields - all methods can see eachother regardless of order - all members in a class can see eachother regardless of modifier + scoping rules for code WITHIN a method - parameters and variables are declared within the method - they cannot be seen OUTSIDE of the method + thus, params and vars declared in method are often called LOCAL VARIABLES + their names, values, and types are inaccessible outside the method) + they can use the same names as fields, because local variables are not seen by the class! + If Java cannot find a local var in a method, then Java looks for a field + they CAN be see by block statements inside the method if declared before the block + scoping for a given block: - variables can be declared inside a block if not declared OUTSIDE (and thus, before) the block - variables declared inside a block do NOT exist outside that block (which summarizes the entire scope issue in the first place!) - blocks can be re-initialized for loops (loop essentially repeat a block) see scope0.java ------------------------------------------------------------------------------- $this$ + want to distinguish between a field and local var if they have the same name? use $this$ + every object has a reference to itself + helps design classes: (1) want to set incoming object's field to current object (2) want to distinguish between field and local var/param (1) English: take an incoming object and set one its fields to the reference to the current object Pseudocode: ref of object1 gets ref of current object (2) methods that have local vars/param with the SAME name as a field will use the local var/param! To distinguish between the two, use $this.name$ inside the method to obtain the field value and not the local var/param! see this0.java ------------------------------------------------------------------------------- Information hiding (Sections 4.2, 5.6): + imagine objects interacting with eachother + need to share and use information + ex) think of CLIENTS (like people) and SERVERS (like a bank) +-----------+ | bank | B | (savings) | +-----------+ A ------------------------------------------------------------------------------- Encapsulation: + splits class into accessible and inaccessible portions (prevents clients from accessing some members) + abstraction: "worry" about higher levels of reasoning and not "nitty-gritty" details + how? modify members with $public$, blank, $protected$, $private$ (skip $protected$ for now; $static$ and $final$ are related, too) + effects on software development: security, reliability of code, ease of reuse ------------------------------------------------------------------------------- $private$ + prevents another class from using a the "." operator to call a member + style: use $private$ for - instance variables (fields that are not modified with $static$) - UTILITY METHODS: methods used only in class (also called SUPPORT METHODS) ------------------------------------------------------------------------------- $public$ + for information that is not "dangerous" to change + style: - class variables (fields that are modified with $static$) - 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 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$) -------------------------------------------------------------------------------