Names - PowerPoint PPT Presentation

About This Presentation
Title:

Names

Description:

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 ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 30
Provided by: TKPr8
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Names


1
Names
  • Scope and Visibility
  • Access Control
  • Packages

2
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
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)

4
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
  • Name conflicts due to multiple inheritance of
    interfaces?
  • class C
  • implements I, J
  • . . .
  • interface I
  • int i
  • interface J
  • int i
  • Ordering of declarations is immaterial except for
    initialization context
  • class C
  • f() g()
  • g() f()
  • int i
  • int j i

6
  • 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
  • Lexical nesting of scopes
  • class C
  • int i
  • int f(int i)
  • return this.i i

7
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
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
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
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
Syntactic Classification of Names
  • Expression name
  • int i argv0 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.

12
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
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
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
(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
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
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
  • 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
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
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

21
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
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
private, protected, public (diff package)
  • 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

24
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
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
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
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
CLASSPATH
(Sub-)Package
Compilation Unit
29
Further Updates
  • Java 1.1 Nested and Inner classes
  • Java 5 Importing static fields, Enumerated types
  • Java 5 Overload resolution Varargs, Co-variant
    types
Write a Comment
User Comments (0)
About PowerShow.com