330 likes | 444 Views
Hiding the Implementation. Maximizing Opportunity for Future Change. The Java Package. Using package, import, and CLASSPATH. public class Functions { public static int two(int i) { return i * 2; } }. public class TestPackage { public static void main(String[] args) {
E N D
Hiding the Implementation Maximizing Opportunity for Future Change
The Java Package Using package, import, and CLASSPATH
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)); } } C:\ my CLASSPATH=. lib util math javac The Default package Everything Compiles and Runs. javac
C:\ CLASSPATH=C:\my public class Functions { public static int two(int i) { return i * 2; } } package lib.math; public class Functions { public static int two(int i) { return i * 2; } } my lib util math javac cannot resolve symbol symbol : variable Functions location: class TestPackage System.out.println("" + Functions.two(4)); ^ javac Adding apackageStatement public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } }
C:\ CLASSPATH=C:\my package lib.math; public class Functions { public static int two(int i) { return i * 2; } } my lib util math javac Everything Compiles and Runs. javac Adding an importStatement public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } } import lib.math.*; public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } }
C:\ CLASSPATH=.;C:\my CLASSPATH=C:\my package lib.math; public class Functions { public static int two(int i) { return i * 2; } } my CLASSPATH=.;C:\my lib util math javac javac ACLASSPATHCatch 22 import lib.math.*; public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } }
C:\ CLASSPATH=.;C:\my package lib.math; public class Functions { public static int two(int i) { return i * 2; } } my CLASSPATH=.;C:\my lib util math javac cannot access Functions bad class file: .\Functions.class class file contains wrong class: lib.math.Functions Please remove or make sure it appears in the correct subdirectory of the classpath. System.out.println("" + Functions.two(4)); ^ javac ACLASSPATHCatch 22 import lib.math.*; public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } }
C:\ CLASSPATH=.;C:\my package lib.math; public class Functions { public static int two(int i) { return i * 2; } } my CLASSPATH=.;C:\my lib util math javac Move TheClass File javac import lib.math.*; public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } }
C:\ CLASSPATH=.;C:\my package lib.math; public class Functions { public static int two(int i) { return i * 2; } } my lib CLASSPATH=.;C:\my util math javac Move TheClass File Everything Compiles and Runs. javac import lib.math.*; public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } }
C:\ CLASSPATH=.;C:\my package lib.math; public class Functions { public static int two(int i) { return i * 2; } } my lib CLASSPATH=.;C:\my util math javac javac AddingApackageStatement import lib.math.*; public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } } package lib.util; import lib.math.*; public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } }
C:\ CLASSPATH=.;C:\my package lib.math; public class Functions { public static int two(int i) { return i * 2; } } my lib CLASSPATH=.;C:\my util math javac package Statement the first non-comment in the file, import Statement(s) next, and class Definition(s) next. javac PlacementRules package lib.util; import lib.math.*; public class TestPackage { public static void main(String[] args) { System.out.println("" + Functions.two(4)); } }
Using The Compiler To Place Class Files C:\ package lib.math; public class Functions { public static int two(int i) { return i * 2; } } my lib util math javac > javac -d C:\my Functions.java > javac -d C:\my *.java
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
A Change of Topic Using package, import, and CLASSPATH Java Access Specifiers
Foo.class new public class Foo { // . . . int i; // . . . fun(){ i = i + 1; } } Access to Everything Access to What? javac class DoFoo { public static void main(String[] args) { Foo f = new Foo(); f.fun(); f.i = f.i + 1; } }
Java Access Specifiers • public • protected • "friendly" • private
CLASSPATH Packages CLASSPATH • 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
Foo f = new Foo(); f.i++; Where Can We Access "i" in Foo From? CLASSPATH CLASSPATH Can this reference access this member? public class Foo { // . . . public int i; // . . . fun(){ i++; } } public class Foo { // . . . public int i; // . . . fun(){ i++; } }
Foo f = new Foo(); f.i++; Where Can We Access "i" in Foo From? CLASSPATH CLASSPATH Can this reference access this member? public class Foo { // . . . public int i; // . . . fun(){ i++; } } public class Foo { // . . . public int i; // . . . fun(){ i++; } }
Foo f = new Foo(); f.i++; public int i; CLASSPATH Y CLASSPATH Y Y Y public class Foo { // . . . public int i; // . . . fun(){ i++; } } public class Foo { // . . . public int i; // . . . fun(){ i++; } } Y Y Y
Foo f = new Foo(); f.i++; int i; CLASSPATH N CLASSPATH N Y Y public class Foo { // . . . int i; // . . . fun(){ i++; } } public class Foo { // . . . int i; // . . . fun(){ i++; } } N N N
Foo f = new Foo(); f.i++; private int i; CLASSPATH N CLASSPATH N N N public class Foo { // . . . private int i; // . . . fun(){ i++; } } public class Foo { // . . . private int i; // . . . fun(){ i++; } } N N N
Inheritance & Access CLASSPATH CLASSPATH
Inheritance & Access obj CLASSPATH CLASSPATH
Class Foo . . . { // Stuff } Class Bar extends Foo { // Stuff } Inheritance & Access obj CLASSPATH CLASSPATH
Foo f = new Foo(); f.i++; Inheritance & Access obj CLASSPATH CLASSPATH public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; } }
Foo f = new Foo(); f.i++; Inheritance & Access obj obj CLASSPATH N CLASSPATH N Y Y public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; } } public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; } } Y N N
public "friendly" private The Member Accessed from anywhere from same class from same package
public public "friendly" "friendly" private protected private The Member Accessed from anywhere from same class from a child class from same package
You May Wish to Order Declarations • public • protected • "friendly" • private
Summary • Access is based on library structure (packages) and inheritance. • The less you expose the more flexibility is preserved