Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 884 (Prasad)Java Names1 Names Scope and Visibility Access Control Packages.

Similar presentations


Presentation on theme: "CS 884 (Prasad)Java Names1 Names Scope and Visibility Access Control Packages."— Presentation transcript:

1 CS 884 (Prasad)Java Names1 Names Scope and Visibility Access Control Packages

2 CS 884 (Prasad)Java Names2 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 )

3 CS 884 (Prasad)Java Names3 Declarations/Definitions package definition (e.g., package one;) single-type import declarationsingle-type import declaration (e.g., import java.io.File;) type-import-on-demand declarationtype-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 definitionformal parameter definition (e.g., catch ( Exception e) {};) local variable definition (e.g., int iterations = 0;)

4 CS 884 (Prasad)Java Names4 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.

5 CS 884 (Prasad)Java Names5 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; }

6 CS 884 (Prasad)Java Names6 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; } }

7 CS 884 (Prasad)Java Names7 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.

8 CS 884 (Prasad)Java Names8 “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; }

9 CS 884 (Prasad)Java Names9 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

10 CS 884 (Prasad)Java Names10 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 {}

11 CS 884 (Prasad)Java Names11 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); package / type / expression 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.

12 CS 884 (Prasad)Java Names12 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.

13 CS 884 (Prasad)Java Names13 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 non- overlapping for loops to have same loop parameter.

14 CS 884 (Prasad)Java Names14 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.

15 CS 884 (Prasad)Java Names15 (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

16 CS 884 (Prasad)Java Names16 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 ; }

17 CS 884 (Prasad)Java Names17 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); }

18 CS 884 (Prasad)Java Names18 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).

19 CS 884 (Prasad)Java Names19 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)

20 CS 884 (Prasad)Java Names20 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; } Access to protected members

21 CS 884 (Prasad)Java Names21 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; }

22 CS 884 (Prasad)Java Names22 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 }

23 CS 884 (Prasad)Java Names23 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 ; }} 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 ; }} private, protected, public ( diff package )

24 CS 884 (Prasad)Java Names24 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.

25 CS 884 (Prasad)Java Names25 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.

26 CS 884 (Prasad)Java Names26 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-type- import declarations. A public type in a package is exported.

27 CS 884 (Prasad)Java Names27 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.

28 CS 884 (Prasad)Java Names28 CLASSPATH = (Sub-)Package = Compilation Unit

29 CS 884 (Prasad)Java Names29 Further Updates Java 1.1: Nested and Inner classes Java 5: Importing static fields, Enumerated types Java 5: Overload resolution : Varargs, Co- variant types


Download ppt "CS 884 (Prasad)Java Names1 Names Scope and Visibility Access Control Packages."

Similar presentations


Ads by Google