package - cs.upt.rocs.upt.ro/~cristina/teaching/oop-en/packages.pdf · java -cp paths_to_bin...
Post on 13-Jan-2020
36 Views
Preview:
TRANSCRIPT
Dr. Petru Florin Mihancea
Packages
package
A
B
<<interface>>X
YZ
C
C1 C2
ro.upt.oop.name3
ro.upt.oop.name1ro.upt.oop.name2
support for organizing a
system into subsystems
(modules)
Dr. Petru Florin Mihancea
APackages: Basic Elements
Dr. Petru Florin Mihancea
packagepackage ro.upt.oop.curs.ceasuri;public interface ClockType {! public void setTime(int h, int n, int s);! public String toString();!}abstract class AbstractClock implements ClockType {
! private int hour, minute, second;
! public AbstractClock() {! ! hour = minute = second = 0;! }! public void setTime(int h, int m, int s) {! ! hour = (h >= 0) && (h < 24) ? h : 0;! ! minute = (m >= 0) && ( m < 60) ? m : 0;! ! second = (s >= 0) && ( s < 60) ? s : 0;! }! public String toString() {! ! return "Current time " + hour + ":" + ! ! ! ! minute + ":" + second; ! }}
package ro.upt.oop.curs.ceasuri;public class Clock extends AbstractClock {!! public String toString() {! ! return "Normal clock - " + super.toString();! }}
package ro.upt.oop.curs;...public class Ceasornicar {! public void regleaza(ClockType x) {! ! x.setTime(12, 0, 0);! }}
compilation units(files); all the contained
classes/interfaces belong to the mentioned package
package name(identif1.identif2. ... identifN)
package is written on the first
non-commented line from a
compilation unit; if it is missing,
the content of the file belongs to
the anonymous package/without
name/default
Dr. Petru Florin Mihancea
complete qualified name
package ro.upt.oop.curs.ceasuri;public interface ClockType {! public void setTime(int h, int n, int s);! public String toString();!}abstract class AbstractClock implements ClockType {
! private int hour, minute, second;
! public AbstractClock() {! ! hour = minute = second = 0;! }! public void setTime(int h, int m, int s) {! ! hour = (h >= 0) && (h < 24) ? h : 0;! ! minute = (m >= 0) && ( m < 60) ? m : 0;! ! second = (s >= 0) && ( s < 60) ? s : 0;! }! public String toString() {! ! return "Current time " + hour + ":" + ! ! ! ! minute + ":" + second; ! }}
package ro.upt.oop.curs;public class Ceasornicar {! public void regleaza(ClockType x) {! ! x.setTime(12, 0, 0);! }}
A package represents also a distinct space name for classes/interfaces; for example,
we might have classes with the same simple name in different packages
packageName.className
package ro.upt.oop.curs;public class Ceasornicar { public void regleaza(ro.upt.oop.curs.ceasuri.ClockType x) {! x.setTime(12, 0, 0); }}
In the same package we do NOT have
ClockType?!?!
Ceasornicar.java:4
: error: cannot find sym
bol
!public vo
id regleaza(ClockType x) {
!
^
symbol: cl
ass ClockType
location: clas
s Ceasornicar
1 error
Dr. Petru Florin Mihancea
Quizzpackage ro.upt.oop.curs.ceasuri;public class Clock extends AbstractClock {!! public String toString() {! ! return "Normal clock - " + super.toString();! }}
In the same package we do NOT have
ClockType ?!?!
Ceasornicar.java:1
1: error: cannot find sym
bol
!public C
lock create() {
!
^
symbol: cl
ass Clock
location: clas
s Test
Ceasornicar.java:1
2: error: cannot find sym
bol
!!
return new Clock();
!!
^
symbol: cl
ass Clock
location: clas
s Test
2 errors
package ro.upt.oop.curs;class Test {! public ro.upt.oop.curs.ceasuri.Clock create() {! ! return new ro.upt.oop.curs.ceasuri.Clock();! }}
package ro.upt.oop.curs;class Test {! public Clock create() {! ! return new Clock();! }}
We have to use the full qualified name
when we declare a reference, create an
object,...
Dr. Petru Florin Mihancea
The alternativepackage ro.upt.oop.curs.ceasuri;public interface ClockType {! public void setTime(int h, int n, int s);! public String toString();!}abstract class AbstractClock implements ClockType {! private int hour, minute, second;! public AbstractClock() {! ! hour = minute = second = 0;! }! public void setTime(int h, int m, int s) {! ! hour = (h >= 0) && (h < 24) ? h : 0;! ! minute = (m >= 0) && ( m < 60) ? m : 0;! ! second = (s >= 0) && ( s < 60) ? s : 0;! }! public String toString() {! ! return "Current time " + hour + ":" + ! ! ! ! minute + ":" + second; ! }}
package ro.upt.oop.curs;import ro.upt.oop.curs.ceasuri.ClockType; public class Ceasornicar {! public void regleaza(ClockType x) {! ! x.setTime(12, 0, 0);! }}
At the beginning of the compilation unit (after package, if it exista)
import packageName.className;import packageName.*;
In the current compilation unit:Version 1 - we can use only the
mentioned class by its short nameVersion 2 -we can use all the classes
defined in the imported package
Dr. Petru Florin Mihancea
Attentionimport ro.upt.oop.curs.ceasuri.*;import tmp.*;...
Clock c; //Ambiguity error
We have another package tmp that contains a class Clock
We need to use the full name of the class or to import the class
1
2Import is exclusively used by the compiler for solving the name of the classes. There is no connection with the #include from C.
3Without a connection with #include, packages...(from java 1.5) existsimport static packageName.className.*;orimport static packageName.className.staticMemberName;for allowing the use (in the file where import static exists) the involved static members without specifying the class they belong to
Dr. Petru Florin Mihancea
Quizz
public class Automat {
! public static void main(String[] args) {! !! }
}
Class String belongs to java.lang and we are allowed to use it without using its complete qualified name or import.
Why the code is compiled?
java.lang is always considered
imported:)
Dr. Petru Florin Mihancea
UML
+regleaza(x : ClockType):void
Ceasornicar
+setTime(h:int,m:int,s:int):void+toString():String
<<interface>>ClockType
Dependency appears also at the classes level
Ceasornicar-ul DOES NOT have ClockType-uri (IS NOT a composition), IS NOT a clock (IS NOT an inheritance relation),but uses references/ methods from ClockType and depends on ClockType
ro.upt.oop.curs.ceasuriro.upt.oop.curs
pachetul
dependențaUn pahet (ro.upt.oop.curs) folosește elemente dintr-un alt pachet (ro.upt.oop.curs.ceasuri)
Dr. Petru Florin Mihancea
BAccess specifiers for
packages content
Dr. Petru Florin Mihancea
Visibility of packages content
publicthe class/interface can be used outside the package it belongs toAttentionthe class should belong to a file (compilation unit) with the same name as the class (followed bt .java); otherwise is a compilation error
default (without no specifier - a keyword does not exist) package access
the class/interface can be used only inside the package it belongs to
the class/interface belongs to
the “interface” of the package
the class/interface is an
implemantation detail of the
package
Dr. Petru Florin Mihancea
Examplespackage pachetA;
public class A { ...}
interface B {! public void aMethod();} package pachetB;
public class Client implements pachetA.B {//Error!! public void doSomething() {! ! pachetA.B x; //Error! ! pachetA.A a;! }
}
The file should be named A.java
package pachetA;
class Whatever implements B {!! public void aMethod() {! ! A a;! ! B b;
...! }}
Dr. Petru Florin Mihancea
CAccess specifiers
for member of classes in packages
the class that contain the
members is visible outside the
package
Dr. Petru Florin Mihancea
Access specifiers
privatethe member of the class (field/method) can be used only inside the class it belongs to
publicthe member of the class (field/method) can be used anywhere
protectedthe member of the class (field/method) can be used inside the class, inside its subclasses or inside the same package
Visibility in UML- private+ public# protected~ package access
default (without no specifier - a keyword does not exist) package access
the member of the class (field/method) can be used only inside the package it belongs to
Dr. Petru Florin Mihancea
Examplespackage pachet1;
public class A1 { private int x; public int y; protected int z; int t;}
package pachet1;
class B1 {! public void metodaB1() { A1 ob = new A1(); ob.x = 1; //Error ob.y = 1; //OK ob.z = 1; //OK ob.t = 1; //OK! } }
package pachet2;class A2 { public void metodaA2() { pachet1.A1 ob = new pachet1.A1(); ob.x = 1; //Error ob.y = 1; //OK ob.z = 1; //Error ob.t = 1; //Error } }class B2 extends pachet1.A1 { public B2() { x = 1; //Error y = 1; //OK z = 1; //OK t = 1; //Error } public void metodaB2() { pachet1.A1 ob = new pachet1.A1(); ob.x = 1; //Error ob.y = 1; //OK ob.z = 1; //Error (An error!) ob.t = 1; //Error } }
Dr. Petru Florin Mihancea
DOrganizing code
(source / machine)
Dr. Petru Florin Mihancea
We have this example...
package ro.upt.oop.curs.ceasuri;public interface ClockType {! public void setTime(int h, int n, int s);! public String toString();!}abstract class AbstractClock implements ClockType {
! private int hour, minute, second;
! public AbstractClock() {! ! hour = minute = second = 0;! }! public void setTime(int h, int m, int s) {! ! hour = (h >= 0) && (h < 24) ? h : 0;! ! minute = (m >= 0) && ( m < 60) ? m : 0;! ! second = (s >= 0) && ( s < 60) ? s : 0;! }! public String toString() {! ! return "Current time " + hour + ":" + ! ! ! ! minute + ":" + second; ! }}
package ro.upt.oop.curs.ceasuri;public class Clock extends AbstractClock {!! public String toString() {! ! return "Normal clock - " + super.toString();! }}
package ro.upt.oop.curs;import ro.upt.oop.curs.ceasuri.*;public class Ceasornicar {! public void regleaza(ClockType x) {! ! x.setTime(12, 0, 0);! }! public static void main(String argv[]) {! ! Clock c = new Clock();! ! Ceasornicar om = new Ceasornicar();! ! om.regleaza(c);! ! System.out.println(c);! }}
Dr. Petru Florin Mihancea
Compiling & Running (I)
javac *.java
java
Exception in thread "main" java.lang.NoClassDefFoundError: Ceasornicar (wrong name: ro/upt/oop/curs/Ceasornicar)! at java.lang.ClassLoader.defineClass1(Native Method)! at java.lang.ClassLoader.defineClass(ClassLoader.java:792)! at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)! at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)! at java.net.URLClassLoader.access$100(URLClassLoader.java:71)! at java.net.URLClassLoader$1.run(URLClassLoader.java:361)! at java.net.URLClassLoader$1.run(URLClassLoader.java:355)! at java.security.AccessController.doPrivileged(Native Method)! at java.net.URLClassLoader.findClass(URLClassLoader.java:354)! at java.lang.ClassLoader.loadClass(ClassLoader.java:424)! at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)! at java.lang.ClassLoader.loadClass(ClassLoader.java:357)! at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)Pepis-MacBook-Pro:src Pepi$
Ceasornicarro.upt.oop.curs.Ceasornicar
Error: Could not find or load main class ro.upt.oop.curs.Ceasornicar
Dr. Petru Florin Mihancea
class files on the diskWe have into a compilaiton unit:
package name1.name2.name3;class X {...}public class Y {...}
On the disk we need to have:
A bin folder (or simillar)• name1 (folder)
• name2 (subfolder name1)• name3 (subfolder name2)
• X.class• Y.class
The compiler automatically distribute the files
javac -d path_to_bin *.java
Dr. Petru Florin Mihancea
ClasspathA frequent argument specific to the compiler, as well as to the virtual machine (-cp)
a path to the folders we distributed the .class files
the elements from the list are separated with : in unix systems and ; in windows systemsex. path1:path2:path3:...:pathN
from this folder the searching of the bytecode of a class will be performed when necessarily; it is based on the complete qualified name of the sub-folders as well as the .class the list may contains also java archives (jar files)
java -cp paths_to_bin ro.upt.oop.curs.Ceasornicar
Dr. Petru Florin Mihancea
Compiling (II)
Similar to the distribution of .class files, the sources(.java files) are distributed on folders/sub-folders according to the package name they
belong to
Dr. Petru Florin Mihancea
Compiling (II)We compile every package taking intoaccount the existing dependencies
ro.upt.oop.curs.ceasuriro.upt.oop.curs
1. javac -d path_to_bin path_to_package1_sources/*.java
2. javac -d path_to_bin -cp path_to_bin_already_compiled path_to_package_surces/*.java
2. javac -d path_to_bin path_to_package2_sources/*.java.javasrc/r
o/upt/oop/curs/Ceasornicar.j
ava:2: error: package
ro.upt.oop.curs.ceasuri does not exist
import ro.upt.oop.curs.ce
asuri.*;
^src/r
o/upt/oop/curs/Ceasornicar.j
ava:4: error: ca
nnot find
symbol
!public vo
id regleaza(ClockType x) {
!
^
symbol: cl
ass ClockType
location: clas
s Ceasornicar
cycling dependencies are a signal to a bad organization of a system.
Dr. Petru Florin Mihancea
Sourcepath
An argument specific to the compiler
a list of folder paths where we distributed source files (.java)
when we compile a compilation unit that depends on classes that have been not compiled, a search in these folders is performed in order to find the source files of these classes (following the subfolders of the classes)
top related