BOOK NOTES CHAPTER 3 BLACK BOXES - black box: device whose - inner workings are hidden - interaction w/ outside world is well-defined - encapsulation - hiding of unimportant details - achieved by taking away inessential features until only essence remains - manage software complexity - higher level data types - object-oriented programming - design objects - use objects - may be done by same or different people - easy to do bad designs - implementation: write the code that turns an idea into software DESIGNING THE PUBLIC INTERFACE OF A CLASS - abstraction: determine essential feature set (commonality) - design so users aren't dependent on your implementation details - express actions/operations with methods - method syntax - method example - method header - access specifier - which methods can use this method - public - always use for now - means any method can use this method - return type - type of output value - keyword void if nothing - otherwise, a type (class or primitive) - method name - list parameters, each with a - inputs to the method - type - name - method body: statements that are executed when the method is called - constructor - defines what the object looks like when it's initially set up - differences from ordinary methods - name must match class name - no return type - syntax - example - class definition - contains constructor and method definitions - syntax - example - public interface of a class - its public constructors and methods - operations any programmer can use to manipulate objects of that class COMMENTING THE PUBLIC INTERFACE - comment behavior of classes and methods as you create - documentation comments - Javadoc program uses these to automatically generate HTML description pages - place before class/method - start with /** - describe purpose - for (public) methods, also - for each parameter - @param - parameter name - description - @return return value description - first sentence is included in summary - provide these comments for everything - update code and documentation together INSTANCE FIELDS - instance field: storage location present in every object of the class - declaration - access specifier - usually private - encapsulation - only methods of the same class can access them - force access through methods - type - name - every object of a class has it's own set of instance fields IMPLEMENTING CONSTRUCTORS AND METHODS - a method body contains a sequence of instructions - constructor - purpose: initialize the fields of an object - what happens when a constructor is called - new object of that class is created - decide which constructor to use by matching parameters - follows statements to initialize instance fields - return an object reference as the value of the new expression - store the object reference in the variable it's assigned to - return statement - specify the value that a method returns - exit the method immediately - return value is the value of the method call expression - syntax - expression optional - what happens when a regular method is called - parameters are copied - statements are executed - return value is the value of the method call expression TESTING A CLASS - no dev env for testing class interactively - write test class instead - test class - has main method - contains statements to test another class - steps (on class being tested) - construct object(s) - invoke method(s) - print result(s) - must associate classes to use them together - make program subfolder - put each class in its own file - put both files in the subfolder - compile both files - run the test program - how to design and implement a class - determine object behavior - specify public interface - set of methods with parameters and return types - constructor(s) - document public interface - determine instance fields: object must have enough internal mem to process method calls in any order - implement constructors and methods: troubles may mean you should rethink your interface - test class CATEGORIES OF VARIABLES - categories - instance fields - local variables - parameter variables - similarities: hold values of specific types - differences - lifetime - instance - occurs for each copy of an object - created when object constructed - exists until no method uses the object - local and parameter - belong to a method - creation - parameter: when method runs - local variable: when declared in method - destroyed when the method completes - initialization - local: must do manually - parameter: automatically when method called - instance - default value supplied - 0 for numbers - null for object references - not caught as an error by the compiler - stylistically, should always do manually in constructor IMPLICIT AND EXPLICIT METHOD PARAMETERS - reference to instance field inside method means that field of the object on which the method was called - use reserved word "this" to refer to implicit parameter - compiler automatically inserts "this." before every instance field used in your code - implicit parameter of any method calls is the same as that of the method containing the call - call one constructor from another - use "this" as the method name, instead of the name of the class - must be the first line of the constructor containing the call