CS100J, Fall 2001 Tues 10/2 Lecture 10 ------------------------------------------------------------------------------- Announcements: + reading: now following chapter 4 for this and next week + P3 due Thurs + may skip any project and do comprehensive makeup at end of semester + T1 handed back either Carpenter or section (will post) + E2 due Thus 10/11 + P4 due 10/18 + T2 on 10/23 ------------------------------------------------------------------------------- Topics: + OO design + syntax of a class + class, object, reference + interaction of classes ------------------------------------------------------------------------------- OOP: + object oriented programming + make aggregate objects with state and behavior + objects interact to simulate physical or abstract process ------------------------------------------------------------------------------- Why? + ease of modeling + spaghetti code: - place chunks/modules of code in separate places + reuse ------------------------------------------------------------------------------- How? + the "research" portion of the programming process + spot the nouns and verbs - nouns: variables, classes - verbs: operators, methods + classes will contain variables and methods (and some other stuff) + methods have variables and operators ------------------------------------------------------------------------------- Syntax of a class: + short version: class name { members } + long version: modifiers $class$ name { modifiers fields; // variables that represent state modifiers constructors; // methods to constructor object modifiers methods; // methods of object's behavior } + modifiers relate to encapsulation, which determines whether or not members are visible outside of the class + for now, generally make - fields $private$ - methods either $private$ or $public$ ------------------------------------------------------------------------------- Classes, objects, references: + OBJECT: a chunk of memory that: - holds info for fields - stores the rules for executing the methods + CLASS: a blueprint for object: - gives code that Java executes when you say $new Classname(stuff)$ - explains how to create a specific object - essentially, a composite type, unlike primitive types which are "whole" + each object is unique - whenever you say $new Classname(stuff)$ Java carves out a new chunk of memory - objects do not share information unless you tell them to (with $static$, which we discuss later) + REFERENCE: variable that: - has type of Classname - stores location (memory address) of an object - may occasionally think of "as the object" but not quite right - longer syntax: Classname reference; reference = new Classname(stuff) - shoter syntax: Classname reference = new Classname(stuff) - super short syntax new Classname(stuff) <-- this code "returns a reference, which is a memory address, to a newly created object" ------------------------------------------------------------------------------- Brief syntax example: // Written file called Person.java: public class Student { int id; Student() {} // return the address of a newly create Student object String label() { return "Student ID: "+ ____ ; } } // Written file called oop0.java: public class oop0 { public static void main(String[] args) { Student ira; // create a new Student object and store address in $ira$: ira = ___ __________ ; // Give the ira object an id of 1234: ira.id = 1234; // Output ira's id with a label: System.out.println(__________________________); } } Q1: how do classes "see" eachother? + save in same project/folder + compiler looks to find other classes there + can put classes in same file, but Java requires that only one be public + CodeWarrior lets you put many public classes in one file Q2: how do the fields get created in the object? + in each object, the fields are created from top-down, left to right when the object is created + the fields are given default values of "zeros" (depends on type) + the fields are given assigned values, if specified Q3: how are the members "seen" WITHIN the class? + the fields can see the others that were declared "before" + the constructor and methods can all "see" all of the fields + each method and constructor can see eachother (can be in any order) Q4: how are the members seen from OUTSIDE of the class? + OUTSIDE classes can not directly "see" the members, except for constructors + you must use the dot (.) operator to access a member - the member must be public, or - have no public/private in front of it (called DEFAULT visibility) + syntax: $reference.member$ - Java looks to see if reference (which is either a reference variable or $new Classname(stuff)$ is valid - Java then checks if member is modifed as $public$ or default visibility - if everything is OK, Java executes the code in the class from which the object was created (that's why a class is a blueprint of an object) ------------------------------------------------------------------------------- Another brief syntax example: // Written file called Person.java: public class Student { private int id; public Student() {} public void setID(int x) { ___ = ____ ; } // set Student's id public int getID() { return _____ ; } // return Student's id } // Written file called oop1.java: public class oop1 { public static void main(String[] args) { // Create a new Student and store in $ira$: Student ira = ____ ____________ ; // Give ira an id: // can't say ira.id = 1234 because id in Class Student is ________ ira._____________________________ ; // output value of id: System.out.println(___________________________); } } ------------------------------------------------------------------------------- Notion of a reference: public class Blah { } public class oop2 { public static void main(String[] args) { new Blah(); // ___________________________ System.out.println(new Blah()); // ______________________________ Blah x = new Blah(); System.out.println(x); // ________________________________ } } -------------------------------------------------------------------------------