+ All Categories
Home > Documents > Hiding the Implementation

Hiding the Implementation

Date post: 06-Jan-2016
Category:
Upload: astro
View: 32 times
Download: 0 times
Share this document with a friend
Description:
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) { - PowerPoint PPT Presentation
33
© 2001 by Ashby M. Woolf Revision 3 Hiding the Implementation Maximizing Opportunity for Future Change
Transcript
Page 1: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

Hiding the Implementation

Maximizing Opportunity for Future Change

Page 2: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

The Java Package

Using package,

import, and

CLASSPATH

Page 3: Hiding the Implementation

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

Page 4: Hiding the Implementation

© 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; }}

Page 5: Hiding the Implementation

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

Page 6: Hiding the Implementation

© 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

Page 7: Hiding the Implementation

© 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

Page 8: Hiding the Implementation

© 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

Page 9: Hiding the Implementation

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

Page 10: Hiding the Implementation

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

Page 11: Hiding the Implementation

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

Page 12: Hiding the Implementation

© 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

Page 13: Hiding the Implementation

© 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

Page 14: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

Using package,

import, and

CLASSPATH

A Change of Topic

Java

Access

Specifiers

Page 15: Hiding the Implementation

© 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

Page 16: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

Java Access Specifiers

• public

• protected

• "friendly"

• private

Page 17: Hiding the Implementation

© 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

Page 18: Hiding the Implementation

© 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?

Page 19: Hiding the Implementation

© 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?

Page 20: Hiding the Implementation

© 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;

Page 21: Hiding the Implementation

© 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;

Page 22: Hiding the Implementation

© 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;

Page 23: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

CLASSPATH

CLASSPATH

Inheritance & Access

Page 24: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

CLASSPATH

CLASSPATH

objInheritance &

Access

Page 25: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

CLASSPATH

CLASSPATH

obj

Class Bar extends Foo { // Stuff}

Class Foo . . . { // Stuff}

Inheritance & Access

Page 26: Hiding the Implementation

© 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

Page 27: Hiding the Implementation

© 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

Page 28: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

The Set View

Page 29: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

The Member Accessedfrom anywhere

from same package

from same class

public "friendly" private

Page 30: Hiding the Implementation

© 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

Page 31: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

You May Wish to Order Declarations

• public

• protected

• "friendly"

• private

Page 32: Hiding the Implementation

© 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

Page 33: Hiding the Implementation

© 2001 by Ashby M. Woolf Revision 3

End of Content


Recommended