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

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Lecture 5: Interfaces.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Java Implementation: Part 2 Version 1.4 of : rearranged slides CompSci 230 Software Construction.
CS 211 Inheritance AAA.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Compilation 2007 Scopes and Environments Michael I. Schwartzbach BRICS, University of Aarhus.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Chapter 12: Adding Functionality to Your Classes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Implementation: Part 3 Software Construction Lecture 8.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS 884 (Prasad)Java Classes1 Classes Fields, methods, and constructors Inheritance.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Name Spaces: ALL versus OOL
Inheritance and Polymorphism
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Nested class.
Object Oriented Programming
Overloading and Constructors
Java Programming Language
Packages and Interfaces
Java – Inheritance.
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

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

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 )

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

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.

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; }

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; } }

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.

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; }

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

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 {}

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.

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.

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.

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.

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

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 ; }

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); }

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

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)

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

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; }

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 }

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 )

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.

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.

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.

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.

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

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