290 likes | 459 Vues
Names. Scope and Visibility Access Control Packages. 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 )
E N D
Names Scope and Visibility Access Control Packages Java Names
Declaration • A declarationintroduces 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) Java Names
Declarations/Definitions • package definition(e.g., packageone;) • single-type import declaration(e.g., importjava.io.File;) • type-import-on-demand declaration (e.g., importjava.io.*;) • class definition (e.g., classPoint{ };) • interface definition (e.g., interfaceColorable{ };) • field definition (e.g., intx_coord;) • method definition (e.g., intdistance(Pointpt){}; ) • formal parameter definition (e.g., catch(Exceptione){};) • local variable definition (e.g., intiterations = 0;) Java Names
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. • “inti = (i = 3) + 2 ;” is legal. Java Names
Ordering of declarations is immaterial except for initialization context class C { f() {g();} g() {f();} int i; int j = i; } Name conflicts due to multiple inheritance of interfaces? class C implements I, J { . . . } interface I { int i; } interface J { int i; } Java Names
Lexical nesting of scopes class C { int i; int f(int i) { return this.i + i; } } Static nesting of scopes class B extends A { static int i; int j; } class C extends B { int i; void f(){ i = B.i + j; } } Java Names
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. Java Names
“Name in Context” The meaning of a name depends on the context of its use.(This permits certain kinds of “multiple definitions”.) packageReuse; classReuse{ Reuse Reuse Reuse(ReuseReuse){ Reuse: for(;;) { if(Reuse.Reuse(Reuse)==Reuse) breakReuse; } returnReuse; } } Java Names
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 fromclassObject Java Names
Syntactic Classification of Names • Method/Constructor name • Integer.parseInt(“15”) • newStreamTokenizer(inFile) • Package name • packageabc.pqr; • importjava.io.*; • importjava.applet.Applet; • Type name • classSquarePanelextendsjava.awt.Button{} Java Names
Syntactic Classification of Names • Expression name • inti =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. Java Names
Resolving Simple Names by Ordering Namespaces • Local variables in a code block, forloop, 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. Java Names
Additional Constraints • Interpretation of keywords static and final asapplied to classes, fields, and methods. • See name resolution for class members. • Disallow hiding of local variables by for loop parameter. However, allow non-overlapping for loops to have same loop parameter. Java Names
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. Java Names
(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 Java Names
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.” complexplus (complex c) { c .real = this . real + c . real ; c . imag = this . imag + c . imag ; return c ; } Java Names
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); } } Java Names
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 classPoint is not accessible outside package points. So super.move(…) is illegal. (No question of overridingmove in PlusPoint.) • Invoking moveAlso on PlusPoint instance invokes move in package Points (and not that in PlusPoints). (No dynamic binding and infinite loop). Java Names
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) Java Names
Access to protected members • The protected members of a subclass object (defined in another package) are not accessible in the superclass. publicclassPoint{ protected int x, y; intdummy (threePoint.Point3D p) { return p.x + p.z ; // one compile-time error } } packagethreePoint; publicclassPoint3DextendsPoint { protected intz; } Java Names
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. packagethreePoint; publicclassPoint3DextendsPoint{ protected intz; public voiddelta (Point p) { p.x += this.x ; p.y += this.y; // compile-time errors } public voiddelta3D (Point3D p) { p.x += this.x ; p.y +=this.y; p.z += this.z; } } Java Names
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 } } Java Names
class S extends C { publicint i; protectedint j; privateint 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 ; }} class C { publicint x; protectedint y; privateint 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 ; }} private, protected, public (diff package) Java Names
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 notin 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. Java Names
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. Java Names
Importing into Compilation Unit • Implicit importjava.lang.* ;in all packages. • Types, but not sub-packages, may be imported. • importjava.io ;is illegal. • Types-imported-on-demand may be hidden by explicit type definitions and single-type-import declarations. • A publictype in a package is exported. Java Names
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 classCare required to be C.java and C.classresp. • The environment variable CLASSPATH contains path to each top-level package (roots of forest oftrees). • Each subdirectory can contain an unnamed package. However, only the one associated with the CurrentWorkingDirectory is available. Java Names
CLASSPATH = (Sub-)Package = Compilation Unit Java Names
Further Updates • Java 1.1: Nested and Inner classes • Java 5: Importing static fields, Enumerated types • Java 5: Overload resolution : Varargs, Co-variant types Java Names