Download presentation
Presentation is loading. Please wait.
Published byWilliam McKenzie Modified over 9 years ago
1
11. Reference Organization Packages
2
What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.
3
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.
4
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?
5
Ambiguous class name conflict... java.awt.List l = new java.awt.List(); java.util.List ll = new java.util.List();
6
Why not just use unique identifiers? Because we can’t control all the programmers who write libraries that we want to use.
7
What is a package? Isolated grouping of reference identifiers (classes and interfaces)
8
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 {}
9
Packages are ALWAYS public! private package foo; // syntax error public package foo; // syntax error protected package foo; //syntax error package foo; // OK.
10
Visibilty Public – world access Protected – sub-class access Default – package access Private – only within the class.
11
ReadOnly Variables public class Customer { private String name = “J. Shmoe”; public String getName() { return name; }
12
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!
13
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!?!?!
14
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.
15
Read-only variable public class Taxes { private int interestRate = 3; public getInterestRate() {// accessor return interestRate; } public void greenSpanManeuver() { interestRate = interestRate + 1; }
16
Write-only Variable public class Taxes { private int interestRate = 3; public void setInterestRate(int r) {// mutator interestRate = r; } public void greenSpanManeuver() { interestRate = interestRate + 1; }
17
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
18
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); }
19
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!
20
How do I share protected elements? package dl; public class ShareMe { protected int i = 10; }
21
Using the protected member package sop; class SomeClass extends dl.ShareMe { SomeClass() { System.out.println(i); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.