© 2001 by Ashby M. Woolf Revision 3
Hiding the Implementation
Maximizing Opportunity for Future Change
© 2001 by Ashby M. Woolf Revision 3
The Java Package
Using package,
import, and
CLASSPATH
© 2001 by Ashby M. Woolf Revision 3
C:\
math
lib
util
my
public class Functions { public static int two(int i) { return i * 2; }}
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
javac
javac
CLASSPATH=.
The Default
package
Everything Compiles and Runs.
© 2001 by Ashby M. Woolf Revision 3
Adding apackage
Statement
CLASSPATH=C:
\my C:\
math
lib
util
my
public class Functions { public static int two(int i) { return i * 2; }}
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
javac
javac
cannot resolve symbolsymbol : variable Functionslocation: class TestPackage System.out.println("" + Functions.two(4)); ^
package lib.math;
public class Functions { public static int two(int i) { return i * 2; }}
© 2001 by Ashby M. Woolf Revision 3
Adding an import
Statement
CLASSPATH=C:
\my C:\
math
lib
util
my package lib.math;
public class Functions { public static int two(int i) { return i * 2; }}
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
javac
javac
import lib.math.*;
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
Everything Compiles and Runs.
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH=C:
\my
ACLASSPATH
Catch 22
CLASSPATH=.;
C:\my C:\
math
lib
util
my package lib.math;
public class Functions { public static int two(int i) { return i * 2; }}
import lib.math.*;
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
javac
javac
CLASSPATH=.;C:\my
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH=.;
C:\my C:\
math
lib
util
my package lib.math;
public class Functions { public static int two(int i) { return i * 2; }}
import lib.math.*;
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
javac
javac
CLASSPATH=.;C:\my
cannot access Functionsbad class file: .\Functions.classclass file contains wrong class: lib.math.FunctionsPlease remove or make sure it appears in the correctsubdirectory of the classpath. System.out.println("" + Functions.two(4)); ^
ACLASSPATH
Catch 22
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH=.;
C:\my C:\
math
lib
util
my package lib.math;
public class Functions { public static int two(int i) { return i * 2; }}
import lib.math.*;
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
javac
javac
CLASSPATH=.;C:\my
Move The
Class File
© 2001 by Ashby M. Woolf Revision 3
Move The
Class File
C:\
math
lib
util
my package lib.math;
public class Functions { public static int two(int i) { return i * 2; }}
import lib.math.*;
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
javac
javac
CLASSPATH=.;
C:\my
CLASSPATH=.;C:\my
Everything Compiles and Runs.
© 2001 by Ashby M. Woolf Revision 3
C:\
math
lib
util
my package lib.math;
public class Functions { public static int two(int i) { return i * 2; }}
import lib.math.*;
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
javac
javac
CLASSPATH=.;
C:\my
CLASSPATH=.;C:\my
AddingA
packageStatement
package lib.util;import lib.math.*;
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
© 2001 by Ashby M. Woolf Revision 3
PlacementRules
C:\
math
lib
util
my package lib.math;
public class Functions { public static int two(int i) { return i * 2; }}
package lib.util;import lib.math.*;
public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); }}
javac
javac
CLASSPATH=.;
C:\my
CLASSPATH=.;C:\my
package Statement the first non-comment in the file,import Statement(s) next, andclass Definition(s) next.
© 2001 by Ashby M. Woolf Revision 3
C:\
math
lib
util
my package lib.math;
public class Functions { public static int two(int i) { return i * 2; }}
javac
> javac -d C:\my Functions.java
Using The Compiler To Place Class Files
> javac -d C:\my *.java
© 2001 by Ashby M. Woolf Revision 3
Keeping Libraries Separate
• Java programs are frequently stored in trees
that begin with reversed domain name
sequences.
• Example:– edu/tamu/math/util/*
– edu/utexas/math/util/*
• Avoids conflicts
© 2001 by Ashby M. Woolf Revision 3
Using package,
import, and
CLASSPATH
A Change of Topic
Java
Access
Specifiers
© 2001 by Ashby M. Woolf Revision 3
class DoFoo {
public static void main(String[] args) {
Foo f = new Foo();
f.fun();
f.i = f.i + 1;
}
}
public class Foo { // . . . int i; // . . . fun(){ i = i + 1; }}
newFoo.class
javac
Accessto
What?
Accessto
Everything
© 2001 by Ashby M. Woolf Revision 3
Java Access Specifiers
• public
• protected
• "friendly"
• private
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
CLASSPATH Packages
• Assume the files are class files
• Assume all the files in the same folder are declared to be in the appropriate package relative to the CLASSPATH
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
public class Foo { // . . . public int i; // . . . fun(){ i++; }}
CLASSPATH
public class Foo { // . . . public int i; // . . . fun(){ i++; }}
Foo f = new Foo();f.i++;
Can this referenceaccess this member?
Where Can We Access "i" in Foo From?
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
public class Foo { // . . . public int i; // . . . fun(){ i++; }}
CLASSPATH
public class Foo { // . . . public int i; // . . . fun(){ i++; }}
Foo f = new Foo();f.i++;
Can this referenceaccess this member?
Where Can We Access "i" in Foo From?
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
public class Foo { // . . . public int i; // . . . fun(){ i++; }}
CLASSPATH
Y
Y
Y
Y
Y
Y
Y
public class Foo { // . . . public int i; // . . . fun(){ i++; }}
Foo f = new Foo();f.i++;
public int i;
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
public class Foo { // . . . int i; // . . . fun(){ i++; }}
CLASSPATH
N
Y
Y
N
N
N
N
public class Foo { // . . . int i; // . . . fun(){ i++; }}
Foo f = new Foo();f.i++;
int i;
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
public class Foo { // . . . private int i; // . . . fun(){ i++; }}
CLASSPATH
N
N
N
N
N
N
N
public class Foo { // . . . private int i; // . . . fun(){ i++; }}
Foo f = new Foo();f.i++;
private int i;
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
CLASSPATH
Inheritance & Access
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
CLASSPATH
objInheritance &
Access
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
CLASSPATH
obj
Class Bar extends Foo { // Stuff}
Class Foo . . . { // Stuff}
Inheritance & Access
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; }}
CLASSPATH
obj
Foo f = new Foo();f.i++;
Inheritance & Access
© 2001 by Ashby M. Woolf Revision 3
CLASSPATH
public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; }}
CLASSPATH
obj
N
Y
Y
Y
N
N
N
public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; }}
obj
Foo f = new Foo();f.i++;
Inheritance & Access
© 2001 by Ashby M. Woolf Revision 3
The Set View
© 2001 by Ashby M. Woolf Revision 3
The Member Accessedfrom anywhere
from same package
from same class
public "friendly" private
© 2001 by Ashby M. Woolf Revision 3
public "friendly" private
The Member Accessedfrom anywhere
from same packagefrom a child class
from same class
protectedpublic "friendly" private
© 2001 by Ashby M. Woolf Revision 3
You May Wish to Order Declarations
• public
• protected
• "friendly"
• private
© 2001 by Ashby M. Woolf Revision 3
Summary
• Access is based on library structure (packages) and inheritance.
• The less you expose the more flexibility is preserved
© 2001 by Ashby M. Woolf Revision 3
End of Content
Top Related