Transcript 11packages

11. Reference Organization
Packages
What is reference organization?
• It is a process that enables us to group
classes and interfaces into isolated
namespaces.
What is an isolated namespace?
• A name is an identifier.
• A namespace is place where identifiers are
grouped.
• An isolated namespace is an isolated
identifier group.
Why isolate identifier groups?
• To enable the reuse of identifiers w/o
causing confusion.
• For example:
–
–
–
–
–
import java.util.List;
import java.awt.List;
....
List l = new List();
// this is an error, how do I fix it?
Ambiguous class name conflict...
• java.awt.List l = new java.awt.List();
• java.util.List ll = new java.util.List();
Why not just use unique
identifiers?
• Because we can’t control all the
programmers who write libraries that we
want to use.
What is a package?
• Isolated grouping of reference identifiers
(classes and interfaces)
How do I create a package?
reserved word “package” appears as the first
java statement in a file.
It appears only once.
ex:
package com.docjava.futil;
public class Foo {}
Packages are ALWAYS public!
•
•
•
•
private package foo; // syntax error
public package foo; // syntax error
protected package foo; //syntax error
package foo; // OK.
Visibilty
•
•
•
•
Public – world access
Protected – sub-class access
Default – package access
Private – only within the class.
ReadOnly Variables
public class Customer {
private String name = “J. Shmoe”;
public String getName() {
return name;
}
}
Why do I need public?
• Publish a constant:
– public static final double PI =
3.141592653589626
• Publish a method
– public static double sin(double x){…}
publish a class
- allows others to use your API!
Why do I need protected?
• You cannot have a protected class.
• You can have protected variables and
methods.
• Only class authors can use protected items.
• Sub-Class authors know what they are
doing!??!? Trust them!?!?!
Why do I need private?
•
•
•
•
Don't trust the subclass author!
Some things can be read but not written.
Some things can be written but not read.
I can control access to state variables.
Read-only variable
public class Taxes {
private int interestRate = 3;
public getInterestRate() {// accessor
return interestRate;
}
public void greenSpanManeuver() {
interestRate = interestRate + 1;
}
}
Write-only Variable
public class Taxes {
private int interestRate = 3;
public void setInterestRate(int r) {// mutator
interestRate = r;
}
public void greenSpanManeuver() {
interestRate = interestRate + 1;
}
}
Why do I need private methods?
•
•
•
•
•
you don't want to publish the method!
Perhaps the spec will change.
public stuff must be stable
public stuff must be supported
public stuff must be documented
Be careful what you make public
• public interface car {
–
–
–
–
–
• }
public int getSpeed();
public int getOdometer();
public int getGas();
public void setAcceleration(int a);
public void setSteerWheelAngle(int degrees);
Default is easy!
• do nothing, it is default!
• Why not make everything default?
– No one can use your stuff!
– Your stuff is unpublished!
– complexity in an OOD is a function of class
interdependence.
– You must encapsulate complexity!
How do I share protected
elements?
package dl;
public class ShareMe {
protected int i = 10;
}
Using the protected member
package sop;
class SomeClass extends dl.ShareMe {
SomeClass() {
System.out.println(i);
}
}