1 / 33

Hiding the Implementation

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) {

astro
Download Presentation

Hiding the Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hiding the Implementation Maximizing Opportunity for Future Change

  2. The Java Package Using package, import, and CLASSPATH

  3. 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

  4. 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)); } }

  5. 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)); } }

  6. 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)); } }

  7. 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)); } }

  8. 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)); } }

  9. 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)); } }

  10. 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)); } }

  11. 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)); } }

  12. 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

  13. 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

  14. A Change of Topic Using package, import, and CLASSPATH Java Access Specifiers

  15. 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; } }

  16. Java Access Specifiers • public • protected • "friendly" • private

  17. 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

  18. 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++; } }

  19. 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++; } }

  20. 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

  21. 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

  22. 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

  23. Inheritance & Access CLASSPATH CLASSPATH

  24. Inheritance & Access obj CLASSPATH CLASSPATH

  25. Class Foo . . . { // Stuff } Class Bar extends Foo { // Stuff } Inheritance & Access obj CLASSPATH CLASSPATH

  26. Foo f = new Foo(); f.i++; Inheritance & Access obj CLASSPATH CLASSPATH public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; } }

  27. 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

  28. The Set View

  29. public "friendly" private The Member Accessed from anywhere from same class from same package

  30. public public "friendly" "friendly" private protected private The Member Accessed from anywhere from same class from a child class from same package

  31. You May Wish to Order Declarations • public • protected • "friendly" • private

  32. Summary • Access is based on library structure (packages) and inheritance. • The less you expose the more flexibility is preserved

  33. End of Content

More Related