DOC PREVIEW
Berkeley COMPSCI 61B - Lecture Notes

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61B Lecture #9Administrative:• 12PM lab today canceled for memorial service. Go tosome other lab this week.Today:• More object-oriented examples: function-like things• Modularization facilities in Java.• More on exceptionsLast modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 1More on Function Parameters• In previous lecture, we saw objects being used likefunctions in Scheme.• In fact, as in Scheme, such objects can carry state.public interface Collector {void collect (Object s);Object value ();}----------------------------------------------------static Object reduce (Collector f, Object[] s) {for (int i = 0; i < s.length; i += 1)f.collect (s[i]);return f.value ();}----------------------------------------------------/** Last string in A, alphabetically */static String max (String[] A){ return (String) reduce (new Maxer (), A); }----------------------------------------------------class Maxer implements Collector {private String max = "";public void collect (Object s){ if (((String) s).compareTo (max) > 0) max = s; }public Object value () { return max; }}Last modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 2Lambda Expressions, Java Style• We had to define a named class, Maxer, to use asfunction-like argument to reduce.• In Scheme, could createanonymousfunctions, as in(map (lambda (x) (1+ x)) list)• In Java, can create single instances of anonymousclasses:static String max (String[] A) {return (String)reduce (new Collector () {private String max = "";public void collect (Object s) {if (((String) s).compareTo(max)>0) max = s;}public Object value () { return max; }}, A);}• The new Collector () { } syntax implements (orextends) Collector and then creates an instantia-tion.• Avoids cluttering up one’s packages with lots of use-less names.• Unfortunately, syntax is awkward—all those types!Last modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 3Package Mechanics• Classes correspond to things being modeled (repre-sented) in one’s program.• Packages are collections of “related” classes and otherpackages.• Java puts standard libraries and packages in packagejava and javax.• By default, a class resides in theanonymous package.• To put it elsewhere, use a package declaration atstart of file, as inpackage mymath;orpackage ucb.io;• javac uses convention that class C in package P1.P2goes in subdirectory P1/P2 of current directory . . .• . . . (or any other directory in theclass path)Last modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 4Access Modifiers• Access modifiers do not add anything to the powerof Java.• Basically allow a programmer to declare what classesare supposed to need to access (“know about”) whatdeclarations.• In Java, are also part of security—prevent program-mers from accessing things that would “break” theruntime system.• Accessibility always determined by static types.Last modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 5The Access Rules• Suppose we have this situation:package P1;class C1 ... {// A member named M,A SomeType M ...}package P2;class C2 ... {void f (P1.C1 x) {... x.M ...} // OK?// C3 is a subtype of C2void g (C3 y) {... y.M ...} // OK?}• Then the access x.M is legal if the access level A is– public, or– protected orpackage private(default—no key-word) and P1is exactly the same package as P2.–package private(the default—no keyword) and P1is exactly the same package as P2, or– private and P1.C1and P2.C2are the same class.• In addition, the access to a protected field of C2—explicitly declared or inherited—is legal within C2 aslong as it is selected from something that is (stati-cally) a subtype of C2. So the access to y.M is legal.• Classes and interfaces may be public or package pri-vate; Fields, methods, and constructors may haveany of the four access levels.• May override a method only with one that hasatleastas permissive an access level [Why?]Last modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 6Intentions of this Design• public declarations representspecifications—whatclients of a package are supposed to rely on.•package privatedeclarations are part of theimple-mentationof a class that must be known to otherclasses that assist in the implementation.• protected declarations are part of the implementa-tion that subtypes may need, but that clients of thesubtypes generally won’t.• private declarations are part of the implementationof a class that only that class needs.Last modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 7Quick Quizpackage P1;public class A1 {int f1() {A1 a = ...a.x1 = 3; // OK?}protected int y1;private int x1;}// Anonymous packageclass A2 {void g (P1.A1 x) {x.f1 (); // OK?x.y1 = 3; // OK?}}class B2 extends A1 {void h (P1.A1 x) {x.f1 (); // OK?x.y1 = 3; // OK?f1(); // OK?y1 = 3; // OK?x1 = 3; // OK?}}Last modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 8Exceptions: Checked vs. Unchecked• The object thrown by throw command must be a sub-type of Throwable (in java.lang).• Java pre-declares several such subtypes, among them– Error, used for serious, unrecoverable errors;– Exception, intended for all other exceptions;– RuntimeException, a subtype of Exception in-tended mostly for programming errors too com-mon to be worth declaring.• Pre-declared exceptions are subtypes of one of these.• Any subtype of Error or RuntimeException isunchecked,meaning simply that any method can throw it.• All other exception types arechecked;methods mustdeclare all checked exception they might throw.Last modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 9Declaring Exceptions• Example of syntax:void myRead () throws IOException { ... }meaning that method myRead or something it callsmight throw an exception of type IOException (orone of its subtypes).• May declare unchecked exceptions as well, but notmandatory.• By declaring throws Exception, can cover all checkedexceptions, but this is not good practice in produc-tion programs.• The throws clause is part of thespecificationof amethod, part of the contract with clients.• When overriding a method, you may not add addi-tional checked exceptions. [Why this rule?]Last modified: Wed Sep 19 10:47:40 2001 CS61B: Lecture #9 10Answers to Quick Quizpackage P1;public class A1 {int f1() {A1 a = ...a.x1 = 3; // OK}protected int y1;private int x1;}// Anonymous packageclass A2 {void g (P1.A1 x) {x.f1 (); // ERRORx.y1 = 3; // ERROR}}class B2 extends A1 {void h (P1.A1 x)


View Full Document

Berkeley COMPSCI 61B - Lecture Notes

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?