Download Explicit Material - Data Structures and Advanced Programming - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!
CS61B Lecture
Announcements:
• Nam will not be holding office hours this Friday (2/17). He is going
to hold make-up office hours 4-6PM this Thur (2/16) in 345 Soda.
Reminders:
• Please use bug-submit for submitting any programming problems you
have with homework and projects.
• You have started Project #1, right?
Today’s Topics:
• Modularization facilities in Java.
• Wrapping up some loose ends and leaving explicit material on Java
for now.
Readings for next week: Assorted Materials on Java, chap. 3 and 5;
Head First Java, chap. 16; Data Structures (Into Java), chap. 1.
Package Mechanics
• Classes correspond to things being modeled (represented) in one’s
program.
• Packages are collections of “related” classes and other packages.
• Java puts standard libraries and packages in package java and javax.
• By default, a class resides in the anonymous package.
• To put it elsewhere, use a package declaration at start of file, as in
package database; or package ucb.util;
• Sun’s javac uses convention that class C in package P1.P2 goes in
subdirectory P1/P2 of any other directory in the class path.
• Unix example:
nova% setenv CLASSPATH .:$HOME/java-utils:$MASTERDIR/lib/classes/junit.jar nova% java junit.textui.TestRunner MyTests
Searches for TestRunner.class in ./junit/textui, ~/java-utils/junit/textui
and finally looks for junit/textui/TestRunner.class in the junit.jar
file (which is a single file that is a special compressed archive of an
entire directory of files).
The Access Rules
• Suppose we have two packages (not necessarily distinct) and two
distinct classes:
package P1; public class C1 ... { // A member named M, A int M ... void h (C1 x) { ... x.M ... } // OK. }
package P2; class C2 extends C3 { void f (P1.C1 x) {... x.M ...} // OK? // C4 a subtype of C2 (possibly C2 itself) void g (C4 y) {... y.M ... } // OK? }
• The access x.M is
– Legal if A is public;
– Legal if A is protected and P 1 is P 2 ;
– Legal if A is package private (default—no keyword) and P 1 is P 2 ;
– Illegal if A is private.
• Furthermore, if C 3 is C 1 , then y.M is also legal under the conditions
above, or if A is protected (i.e., even if P 1 is not the same as P 2 ).
What May be Controlled
• Classes and interfaces that are not nested may be public or package
private (we haven’t talked explicitly about nested types yet).
• Members—fields, methods, constructors, and (later) nested types—
may have any of the four access levels.
• May override a method only with one that has at least as permissive
an access level.
– Reason: avoid inconsistency:
package P1; | package P2; public class C1 { | class C3 { public int f () { ... } | void g (C2 y2) { } | C1 y1 = y | y2.f (); // Bad??? public class C2 extends C1 { | y1.f (); // OK??!!? // Actually a compiler error; pretend | } // it’s not and see what happens | } int f () { ... } }
– That is, there’s no point in restricting C2.f, because access con-
trol depends on static types, and C1.f is public.
Quick Quiz
package SomePack; public class A1 { int f1() { A1 a = ... a.x1 = 3; // OK? } protected int y1; private int x1; }
// Anonymous package
class A2 { void g (SomePack.A1 x) { x.f1 (); // OK? x.y1 = 3; // OK? } }
class B2 extends A1 { void h (SomePack.A1 x) { x.f1 (); // OK? x.y1 = 3; // OK? f1(); // OK? y1 = 3; // OK? x1 = 3; // OK? } }
• Note: Last three lines of h have implicit this.’s in front. Static type
of this is B2.
Quick Quiz
package SomePack; public class A1 { int f1() { A1 a = ... a.x1 = 3; // OK } protected int y1; private int x1; }
// Anonymous package
class A2 { void g (SomePack.A1 x) { x.f1 (); // OK? x.y1 = 3; // OK? } }
class B2 extends A1 { void h (SomePack.A1 x) { x.f1 (); // OK? x.y1 = 3; // OK? f1(); // OK? y1 = 3; // OK? x1 = 3; // OK? } }
• Note: Last three lines of h have implicit this.’s in front. Static type
of this is B2.
Quick Quiz
package SomePack; public class A1 { int f1() { A1 a = ... a.x1 = 3; // OK } protected int y1; private int x1; }
// Anonymous package
class A2 { void g (SomePack.A1 x) { x.f1 (); // ERROR x.y1 = 3; // ERROR } }
class B2 extends A1 { void h (SomePack.A1 x) { x.f1 (); // OK? x.y1 = 3; // OK? f1(); // OK? y1 = 3; // OK? x1 = 3; // OK? } }
• Note: Last three lines of h have implicit this.’s in front. Static type
of this is B2.
Quick Quiz
package SomePack; public class A1 { int f1() { A1 a = ... a.x1 = 3; // OK } protected int y1; private int x1; }
// Anonymous package
class A2 { void g (SomePack.A1 x) { x.f1 (); // ERROR x.y1 = 3; // ERROR } }
class B2 extends A1 { void h (SomePack.A1 x) { x.f1 (); // ERROR x.y1 = 3; // OK? f1(); // OK? y1 = 3; // OK? x1 = 3; // OK? } }
• Note: Last three lines of h have implicit this.’s in front. Static type
of this is B2.
Quick Quiz
package SomePack; public class A1 { int f1() { A1 a = ... a.x1 = 3; // OK } protected int y1; private int x1; }
// Anonymous package
class A2 { void g (SomePack.A1 x) { x.f1 (); // ERROR x.y1 = 3; // ERROR } }
class B2 extends A1 { void h (SomePack.A1 x) { x.f1 (); // ERROR x.y1 = 3; // OK? f1(); // ERROR y1 = 3; // OK x1 = 3; // OK? } }
• Note: Last three lines of h have implicit this.’s in front. Static type
of this is B2.
Quick Quiz
package SomePack; public class A1 { int f1() { A1 a = ... a.x1 = 3; // OK } protected int y1; private int x1; }
// Anonymous package
class A2 { void g (SomePack.A1 x) { x.f1 (); // ERROR x.y1 = 3; // ERROR } }
class B2 extends A1 { void h (SomePack.A1 x) { x.f1 (); // ERROR x.y1 = 3; // OK? f1(); // ERROR y1 = 3; // OK x1 = 3; // ERROR } }
• Note: Last three lines of h have implicit this.’s in front. Static type
of this is B2.
Access Control Static Only
“Public” and “private” don’t apply to dynamic types; it is possible to call
methods in objects of types you can’t name:
package utils; | package mystuff; /** A Set of things. */ | public interface Collector { | class User { void add (Object x); | Collector c = } | utils.Utils.concat (); ---------------------------- | package utils; | c.add ("foo"); // OK public class Utils { | ... c.value (); // ERROR public static Collector concat () { | ((utils.Collector) c).value () return new Concatenator (); | // ERROR } | } ----------------------------------
/** NON-PUBLIC class that collects strings. */ class Concatenater implements Collector { StringBuffer stuff = new StringBuffer (); int n = 0; public void add (Object x) { stuff.append (x); n += 1; } public Object value () { return stuff.toString (); } }
Loose End #1: Importing
• Writing java.util.List every time you mean List or java.lang.regex.Pattern
every time you mean Pattern is annoying.
• The purpose of the import clause at the beginning of a source file is
to define abbreviations:
– import java.util.List; means “within this file, you can use List
as an abbreviation for java.util.List.
– import java.util.*; means “within this file, you can use any
class name in the package java.util without mentioning the pack-
age.”
• Importing does not grant any special access; it only allows abbrevi-
ation.
• In effect, your program always contains import java.lang.*;
Loose End #3: Nesting Classes
• Sometimes, it makes sense to nest one class in another. The nested
class might
– be used only in the implementation of the other, or
– be conceptually “subservient” to the other
• Nesting such classes can help avoid name clashes or “pollution of the
name space” with names that will never be used anywhere else.
• Example: Polynomials can be thought of as sequences of terms.
Terms aren’t meaningful outside of Polynomials, so you might define
a class to represent a term inside the Polynomial class:
class Polynomial {
methods on polynomials
private Term[] terms; private static class Term { ... } }
Inner Classes
• Last slide showed a static nested class. Static nested classes are
just like any other, except that they can be private or protected,
and they can see private variables of the enclosing class.
• Non-static nested classes are called inner classes.
• Somewhat rare (and syntax is odd); used when each instance of the
nested class is created by and naturally associated with an instance
of the containing class, like Banks and Accounts:
Bank
account
account
Bank
account
account
class Bank { | Bank e = new Bank(...); private void connectTo (...) {...} | Bank.Account p0 = public class Account { | e.new Account (...); public void call (int number) { | Bank.Account p1 = Bank.this.connectTo (...); ... | e.new Account (...); } // Bank.this means "the bank that | } // created me" | } |