Beruflich Dokumente
Kultur Dokumente
Packages in Java
A package is a collection of classes (a library ). In a file the first line must specify the package, e.g.,
n
package mypackage;
Characteristic of a package
n
Organized in a hierarchy
u u
Uses the file system for implementing the hierarchy A package corresponds to a directory
By default, classes belong to the unnamed package. Packages introduce new scope rules.
OOP: Object-Oriented Programming, Part 2 2
Tied to local directory structure Case sensitive Default package in current directory.
Package Example
package com.mycompany.misc; // file Car.java public class Car { public Car(){ System.out.println ("com.mycompany.misc.Car"); } } package com.mycompany.misc; // file Truck.java public class Truck { public Truck(){ System.out.println ("com.mycompany.misc.Truck"); } }
mypackage.myClass mypackage1.mypackage2.mypackage3.myOtherClass In a file import mypackage1.mypackage2.mypackage3.*, then, myOtherClass does not have to be qualified.
from com.mycompany.misc
from java.math
CLASSPATH
Store misc package in /user/torp/java/com/mycompany/misc
directory, i.e., the files Car.class and Truck.class.
Information Hiding
Separate interface from implementation How much should a user of a class see? Rules of thumb
n n
Variable/method is private to the class. "Visible to my self". Variable/method can be seen by the class, all subclasses, and other classes in the same package. "Visible to the family" or "beware of dog". Variable/method can be seen by all classes. "Visible to all".
protected
n n
public
n n
"Friendly"
Default access, has no keyword. public to other members of the same package. private to anyone outside the package. Also called package access.
10
Public/"Friendly" Example
package com.mycompany.misc; public class Car { public Car(){ System.out.println ("com.mycompany.misc.Car"); } void foo () { System.out.println ("foo"); } }
// in another package import com.mycompany.misc.*; public static void main (String args[]){ Car car1 = new Car(); car1.foo; // compile error "private" in this package }
11
Private Example
class Singleton { private int myData = 42; private static Singleton s = new Singleton(); // make default constructor private private Singleton(){ } static Singleton getSingleton () { return s; } void singletonMethod() { /* do stuff */ }; int getSingletonData { return myData } } public class UseSingleton { public static void main (String args[]){ Singleton s1 = new Singleton(); // compile error Singleton s2 = getSingleton(); s2.singletonMethod(); }
OOP: Object-Oriented Programming, Part 2 12
Controlled access to one instance Reduced name space (not a "global" variable) Permits refinement of operations and representation Permits a variable number of instances More flexible than static methods
13
Not supported in Java! Default see the slide on friendly Not supported in Java! Can be seen by all other classes in other packages Only one public class per file A class without an access modified can from classes within the same package. What would it mean?
14
protected
n
public
n n
"Friendly"
n
Tools
The Java Development Kit (JDK) contains a set of tools useful
for Java programmers. javac
n n n
The Java compiler Translates Java source code to byte code javac [options] <filename.java> The Java interpreter Interprets Java byte code java [options] <filename.class> The Java documentation generator javadoc [options] <filename.class> | <package>
15
java
n n n
javadoc
n n
Javadoc, Example
import com.mycompany.misc.*; /** Implements a garage consisting of cars and trucks */ public class Garage { /** Comment on instance variable */ Car car1; Truck truck1; /** Default constructor, create a car and a truck */ public Garage(){ /** Comment on method internals */ car1 = new Car(); truck1 = new Truck(); } /** Prints the vehicles made in the constructor @see Garage#Garage() */ public void toPrint(){ System.out.println ("A garage: " + car1 + truck1); } }
OOP: Object-Oriented Programming, Part 2 16
Class Properties
A class variable is a variable that is common to all instances of
the same class. A class method is a method that operates on a class as it was an object.
Class variables are stored in metaobjects Java supports metaobject via the class Class. Further there is a set of classes in the package java.lang.reflect. See Chapter 12 "RunTime Type Identification".n
17
18
Summary
Package, the library unit in Java. Access modifiers
n
Tells clients what they can and cannot. Very important in design (and implementation).
Decomposing system into objects. Defining the public interface of the objects. Finding out what is likely to change. Finding out what is likely to stay the same.
19