Transcript Names

Names
Scope and Visibility
Access Control
Packages
CS 884 (Prasad)
Java Names
1
Declaration
• A declaration introduces an entity into
a Java program and includes an identifier
that can be used in a name.
• A name is used to refer to an entity
declared in a Java program.
– Simple Name (identifier)
– Qualified Name (period-separated sequence of identifiers)
CS 884 (Prasad)
Java Names
2
Declarations/Definitions
• package definition (e.g., package one;)
• single-type import declaration (e.g., import java.io.File;)
• type-import-on-demand declaration (e.g., import java.io.*;)
• class definition (e.g., class Point { };)
• interface definition (e.g., interface Colorable { };)
• field definition (e.g., int x_coord;)
• method definition (e.g., int distance (Point pt) {}; )
• formal parameter definition (e.g., catch (Exception e) {};)
• local variable definition (e.g., int iterations = 0;)
CS 884 (Prasad)
Java Names
3
Scope of a declaration
• Region of program text within which the declared
entity can be referred to using simple name.
– scope of package is host system dependent.
– scope of imported types is the compilation unit.
– scope of class/interface types is the package.
– scope of fields/methods is the entire class/interface.
• The declaration must precede use in a field initialization expression.
– scope of parameters/local vars is the rest of the block.
• “int i = (i = 3) + 2 ;” is legal.
CS 884 (Prasad)
Java Names
4
• Ordering of
declarations is
immaterial except for
initialization context
class C {
f() {g();}
g() {f();}
int i;
int j = i;
}
CS 884 (Prasad)
• Name conflicts due to
multiple inheritance of
interfaces?
class C
implements I, J {
. . . }
interface I {
int i;
}
interface J {
int i;
}
Java Names
5
• Lexical nesting of
scopes
• Static nesting of
scopes
class C {
int i;
int f(int i) {
return this.i + i;
}
}
class B extends A {
static int i;
int j;
}
class C extends B {
int i;
void f(){
i = B.i + j;
}
}
CS 884 (Prasad)
Java Names
6
Resolving Names
• Scopes can overlap. (Same name for different entities.)
• To disambiguate a name:
– Use contextual information to determine if name
refers to a package, a type, a method, a label, a
variable (field/local/formal) etc.
– Use signature to resolve method names.
– Within nested scopes, type names, variable names &
method names resolved by shadowing, hiding, or
overriding.
• Otherwise, “Ambiguity error”.
• Possible remedy: use fully qualified name.
CS 884 (Prasad)
Java Names
7
“Name in Context”
The meaning of a name depends on the context of its use.
(This permits certain kinds of “multiple definitions”.)
package Reuse;
class Reuse {
Reuse Reuse Reuse(Reuse Reuse) {
Reuse : for (;;) {
if (Reuse . Reuse (Reuse) == Reuse)
break Reuse;
}
return Reuse;
}
}
CS 884 (Prasad)
Java Names
8
Members (Cf. Namespaces)
• Package
– sub-package, class, interface
• every member of a package has a unique name.
• Class type
– fields and (non-constructor) methods, declared or inherited
• hiding, overriding, overloading
• Interface type
– constants and method signatures, declared or (multiply)
inherited
• ambiguity resolved using qualified names
• Array type
– length, plus those inherited from class Object
CS 884 (Prasad)
Java Names
9
Syntactic Classification of Names
• Method/Constructor name
• Integer.parseInt(“15”)
• new StreamTokenizer(inFile)
• Package name
• package abc.pqr;
• import java.io.*;
• import java.applet.Applet;
• Type name
• class SquarePanel extends java.awt.Button {}
CS 884 (Prasad)
Java Names
10
Syntactic Classification of Names
• Expression name
– int i = argv[0] + j++;
• Ambiguous name
–
Local.field = Package.Package.Class.field;
– Parameter.field.field = Class. method() + Local.method(1,2);
An ambiguous name is reclassified as package / type /
expression name. Reclassification uses declarations and
additional scoping rules. In Java 1.0, it implicitly
assumed that classes cannot be nested.
CS 884 (Prasad)
Java Names
11
Resolving Simple Names by Ordering
Namespaces
• Local variables in a code block, for loop, or the
parameters to an exception handler.
• Parameters to method or constructor.
• Fields of enclosing class or interface.
• Class or interface types in the compilation unit.
• Explicitly named imported types in the compilation unit.
• Other class or interface types in the other compilation
units of the same package.
• Implicitly named imported types in the compilation unit.
• Packages available on the host system.
CS 884 (Prasad)
Java Names
12
Additional Constraints
• Interpretation of keywords static and final
as applied to classes, fields, and methods.
• See name resolution for class members.
• Disallow hiding of local variables by for
loop parameter. However, allow nonoverlapping for loops to have same loop
parameter.
CS 884 (Prasad)
Java Names
13
Access Control
Access control is different from scope; it
specifies the part of the program text within
which the declared entity can be referred to
by a qualified name.
In the context of classes, it applies to access
to class members by qualified names, in
subclasses, and to the constructor
invocation.
CS 884 (Prasad)
Java Names
14
(cont.)
• Class / Interface type
• public : wherever the package is accessible.
• default : local to the package.
• Members of Reference type
• public : wherever the type is accessible.
• protected : accessible in the package, class and its subclasses
• default : accessible in the package
• private : accessible in the class
CS 884 (Prasad)
Java Names
15
Access to private members
• Private members of an object are accessible
to other objects of the same class.
Objects are not “shielded” from each other.
“Privacy is only class deep.”
complex plus (complex c) {
c . real = this . real + c . real ;
c . imag = this . imag + c . imag ;
return c ;
}
CS 884 (Prasad)
Java Names
16
Default Access to members
package points;
public class Point {
public int x, y;
void move(int dx, int dy) {
x += dx;
y += dy;
}
public void moveAlso(int dx, int dy) {
move(dx, dy);
}
}
CS 884 (Prasad)
Java Names
17
package morepoints;
public class PlusPoint extends
points.Point {
public void move(int dx, int dy) {
//super.move(dx, dy); // compile-time error
moveAlso(dx, dy);
}
}
• move in class Point is not accessible outside
package points. So super.move(…) is illegal.
(No question of overriding move in PlusPoint.)
• Invoking moveAlso on PlusPoint instance
invokes move in package Points (and not that in
PlusPoints). (No dynamic binding and infinite loop).
CS 884 (Prasad)
Java Names
18
private Method: Accessibility and Overriding
• A method can be overridden only if it is
accessible.
– A private method of a class cannot be
overridden in a subclass.
– A private method is not accessible outside its
own class, so an invocation of a private method
always invokes the same implementation. (cf.
dynamic binding)
CS 884 (Prasad)
Java Names
19
Access to protected members
• The protected members of a subclass object (defined in another
package) are not accessible in the superclass.
public class Point {
protected int x, y;
int dummy (threePoint.Point3D p) {
return p.x + p.z ; // one compile-time error
}
}
package threePoint;
public class Point3D extends Point {
protected int z;
}
CS 884 (Prasad)
Java Names
20
Access to protected members
• The protected members of a class are accessible to the code in a
subclass (outside the package) only when the members belong to the
subclass object being implemented.
package threePoint;
public class Point3D extends Point {
protected int z;
public void delta (Point p) {
p.x += this.x ; p.y += this.y; // compile-time errors
}
public void delta3D (Point3D p) {
p.x += this.x ; p.y += this.y; p.z += this.z;
}
}
CS 884 (Prasad)
Java Names
21
Motivating protected Restriction
• The protected members of a class are accessible to the
code in a subclass (outside the package) only when the
members belong to the subclass object being implemented.
• Otherwise:
class Secret {
// in: package A;
protected int i;
}
class Sneaky extends Secret {// in: package B;
public int violateProtection (Secret s) {
return (s.i);
// compile-time error
}
}
CS 884 (Prasad)
Java Names
22
private, protected, public (diff package)
class C {
public
int x;
protected int y;
private
int z;
int p(C c) {
return c.x + c.y +
c.z
;
}
int q(S s) {
return s.x + s.y
// + s.z
+ s.i ;
// + s.j + s.k ;
class S extends C {
public
int i;
protected int j;
private
int k;
int p(C c) {
return c.x ;
// + c.y + c.z ;
}
int q(S s) {
return s.x + s.y +
// s.z +
s.i + s.j +
s.k ;
}}
}}
CS 884 (Prasad)
Java Names
23
Access control : super
• The protected constructors of a class are
accessible to a subclass (outside the
package) through the use of super in a
constructor definition, but not in a class
instantiation expression.
• The protected methods of a class are
accessible to a subclass (outside the
package) using its name when it is
inherited, or using super (in method
definitions) when it is overridden.
CS 884 (Prasad)
Java Names
24
Packages
•
•
•
•
Java Program : set of packages.
Package : set of compilation units.
Package Members: sub-packages and types.
Compilation unit with no package
declaration is part of unnamed package.
• There is no special access relationship
between a package and a sub-package.
CS 884 (Prasad)
Java Names
25
Importing into Compilation Unit
• Implicit import java.lang.* ; in all packages.
• Types, but not sub-packages, may be imported.
• import java.io ; is illegal.
• Types-imported-on-demand may be hidden
by explicit type definitions and single-typeimport declarations.
• A public type in a package is exported.
CS 884 (Prasad)
Java Names
26
Packages to UNIX File System
• (Sub) Packages are mapped to (sub) directories.
• p.q. ... to p/q/ ...
• The names of Java source file and the
corresponding bytecode file for public class C are
required to be C.java and C.class resp.
• The environment variable CLASSPATH contains
path to each top-level package (roots of forest of
trees).
• Each subdirectory can contain an unnamed
package. However, only the one associated with
the CurrentWorkingDirectory is available.
CS 884 (Prasad)
Java Names
27
CLASSPATH
= (Sub-)Package
= Compilation Unit
CS 884 (Prasad)
Java Names
28
Further Updates
• Java 1.1: Nested and Inner classes
• Java 5: Importing static fields, Enumerated
types
• Java 5: Overload resolution : Varargs, Covariant types
CS 884 (Prasad)
Java Names
29