CS100J, Fall 2001 Thurs, 10/11 Lecture 11 ------------------------------------------------------------------------------- Announcements: + reading: still chapters 4 and 5 + T1 in Carpenter Thurs, about 4pm + P4 posted later tonight/tomorrow + E2 due Thus 10/11 + P4 due 10/18 + T2 on 10/23 ------------------------------------------------------------------------------- Summary: + OOP: - model physical and abstract things - nouns become vars or classes - verbs become ops or methods + writing classes: think about what it "has" or if it is "whole" - things with parts become classes - things that are whole/indivible usually become variables - classes contain/have other "nouns" and "verbs" to model state and behavior of the objects that are created from the class + syntax: - classes are essentially types that you add to the language - within a class: members are visible to all other members - outside class: + public members are visible to members of other class + private members cannot be accessed by member of other class ------------------------------------------------------------------------------- Topics: + references + $toString$ + "Has-a" relationships: + aliases + $this$ + scope ------------------------------------------------------------------------------- References: + $new classname(params)$ is the address of the object Java creates using $classname$ + the value of the address is $Classname@something$ - you can think of it as a string - but you don't usually need or use the actual value - just realize that it IS a value - references sometimes use a "zero" address which called $null$ + Classname ref = new classname(params) - stores the reference in the REFERENCE VARIABLE (a variable that contains a reference) - $new constructor(params)$ creates the object and gives it an address - $Classname ref$ gets that address and now REFERS to object - the reference variable does NOT store the address! + since the address is indeed a value, you may print it - the actual value is useless - so, Java lets you "print an object's contents" with builtin method $toString$: - put a PUBLIC method $toString$ inside your class that returns a String - whenever you print the reference to an object ex) System.out.println(new Thing()) Thing t=new Thing(); System.out.println(t) you get the returned value from $toString$ references.java ------------------------------------------------------------------------------- Aggregation/"has-a" relationships + reminder: classes HAVE members, which are "nouns" and "verbs" + a composite thing will generally have components which are other things + so, fields of a class may be references to other objects! aggregation.java ------------------------------------------------------------------------------- Aliases + references are values, which are addresses + the address of an object can be copied from variable to variable! + ALIAS: a reference variable that has the address of another object ex) Thing t1 = new Thing(); Thing t2 = new Thing(); // t1 and t2 refer to different objects! t1 = t2; // t1 gets the address of the object referred to by t2 alias0.java ------------------------------------------------------------------------------- $this$ + special kind of reference + 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! this0.java ------------------------------------------------------------------------------- Scope: + scope is level of visibility (actually, "location of visibility") + need to keep track of what's visible from different points in the code class { fields; methods(params) { vars; { block vars; } } } class: - fields seen by eachother, all code in all methods, params&vars, blocks - if method's params&vars use the same name as the field, that's OK - need $this$ to distinguish between them - $this$ means "reference to the current object" - $public$, $private$ discussed in summary and next lecture method: - params&vars are "born","live", and "die" in the methods - params&vars (same level of scope) seen by the method and all blocks inside the method - params&vars NOT accessible or visible outside of the method block statement: - the block can see variables at the method scope but not other blocks outside of the current block - you can "repeat" a block using a loop, e.g., while(c) { int x; .... } Everything: - cannot redeclare a variable within the same scope - look for the braces! -------------------------------------------------------------------------------