External Scope CECS 277 Mimi Opkins.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

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.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Learners Support Publications Classes and Objects.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Introduction to Generics
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CITA 342 Section 1 Object Oriented Programming (OOP)
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
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Introduction to Object-oriented Programming
Java Generics.
Object-Oriented Concepts
Summary prepared by Kirk Scott
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
Static data members Constructors and Destructors
Object-Oriented Programming: Classes and Objects
Inheritance and Polymorphism
Table of Contents Class Objects.
11.1 The Concept of Abstraction
Classes & Objects.
Object-Oriented Programming: Classes and Objects
Nested class.
Abstract Data Types and Encapsulation Concepts
Chapter 9 Inheritance and Polymorphism
Chapter 6 Methods: A Deeper Look
Packages and Interfaces
Chapter 12 Abstract Classes and Interfaces
Classes and Objects.
Java – Inheritance.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Fundaments of Game Design
Chapter 11 Inheritance and Polymorphism Part 1
Visibilities and Static-ness
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Exceptions.
CSG2H3 Object Oriented Programming
11.1 The Concept of Abstraction
Presentation transcript:

External Scope CECS 277 Mimi Opkins

External Scope Access Rules Java allows class variable/methods to be accessed from three external places: derived classes, variable/methods of the same package, and code external to the package.

Packages A package contains a set of related classes, and sometimes we want to make variable/methods of those classes accessible to each other, even if they aren’t public. For example, suppose that a Date class is part of a calendar package. Another class, called ShowWeek, in the same package displays a week surrounding a given date. Both of the classes use the Julian day as their internal representation. It is more efficient for a Date object to make its Julian day variable/method directly accessible to ShowWeek than to require conversion of the date first to its external form and then back to a Julian day in the other object. The user is unaware of this shortcut, so the encapsulation is preserved.

Access Levels Classes naturally belong together in a package if they have common internal representations. In that case, they can bypass each other’s encapsulations because the common details of their implementations are already known to anyone who’s working with them. Java defines four levels of access for class variable/methods, three of which enable direct access by other package variable/methods. The four levels of access are public protected default (package) private There are keywords that we use as access modifiers for each of these levels except the package level, which is the default level. If you omit an access modifier from the declaration of a class variable/method, it is at the package level of access by default.

Public Access A public variable/method can be accessed from anywhere outside of the class. User code, derived classes, and other classes in the same package can all access a public variable/method. The variable/method may still be hidden by a declaration of the same name in another class, in which case references to it must be qualified with its class or instance name. Here’s an example of using qualified names. If the class ShowWeek defines a julianDay field and Date also defines julianDay as a static field, then ShowWeek would need to refer to Date.julianDay to access the static field of Date. If julianDay is an instance field of Date and the particular object is called instanceName, then ShowWeek must refer to it as follows: instanceName.julianDay

Protected Access A protected variable/method is accessible to classes in the same package and can be inherited by derived classes outside of the package. Code that is outside of the package can only inherit protected variable/methods of a class; it can’t access them directly. In the following code segment, we define two packages. The second package has a class (DerivedClass) that is derived from the class in the first package (SomeClass). DerivedClass inherits the protected field someInt from SomeClass. Notice that DerivedClass doesn’t include a declaration of someInt. DerivedClass defines a method that has one parameter of the class SomeClass and another parameter of its own class. It then tries to access the someInt field in both parameters.

package one; public class SomeClass { protected int someInt; }

import one.*; package two; public class DerivedClass extends SomeClass { void demoMethod(SomeClass param1, DerivedClass param2) param1.someInt = 1; // Generates a compiler error // Can't access variable/method of instance of SomeClass param2.someInt = 1; // This access is legal // It refers to the inherited variable/method }

The compiler will issue an error message for the first assignment statement because it is illegal to access the protected field of a superclass when the superclass resides in a different package. The second assignment is valid because it refers to the inherited field within DerivedClass. The protected modifier provides the least restrictive level of access that isn’t public. We use protected to enable users to extend our class with a subclass. The subclass inherits its own copies of the protected variable/methods, but cannot access the originals. If a package of classes is independent of an application (such as the java.util package), it is often desirable to enable users to derive their own classes from the library classes.

Package Access When no access modifier is specified, then classes in the same package can access the variable/method. No other external access is allowed. A variable/method at the package level of access cannot be inherited by a derived class in another package. Up to this point, we’ve given only examples in which every variable/method of a class is inherited by a subclass. With the introduction of the package level of access, we see that Java also lets us selectively restrict the variable/methods that are inherited. A derived class in the same package retains access to the variable/methods of its superclass that are at the package level of access. All classes within the same package have access to each other’s public, protected, and package variable/methods. Here’s the preceding example, but with both classes in the same package and someInt at the package level of access. In this case, both assignment statements are valid.

package one; public class SomeClass { int someInt; } public class DerivedClass extends SomeClass void demoMethod(SomeClass param1, DerivedClass param2) param1.someInt = 1; param2.someInt = 1;

Private Access Lastly, the private modifier cuts off all external access, even by classes in the same package. A private variable/method of a class can be referred to only by other variable/methods of the class, and only the internal scope rules apply to it. It isn’t even permissible for a derived class in the same package to access a private variable/method of its superclass. Instances of a class can refer to each other’s private variable/methods. A variable/method is private to its class, which includes all instances of the class. Thus two objects, someObj and otherObj, that have private int fields called someInt can refer to each other’s fields. That is, someObj can refer to otherObj.someInt and otherObj can refer to someObj.someInt. Within a method, all local identifiers are automatically private; Java doesn’t allow us to declare them with access modifiers. The reason is that the lifetime of a local identifier is limited to a particular method call, so it simply doesn’t exist when external code is running.

So far, we have primarily used the public and package levels of access, keeping data variable/methods at the package level and making most methods and classes public. This simple scheme provides encapsulation and a consistent user interface that is strictly a set of method calls. However, this scheme is inflexible and limits our ability to provide for either extension or the construction of related classes that are grouped into a package.

Which access modifier do I use? Now that we have a better understanding of Java’s access rules, we must consider which access modifier is most appropriate for each variable/method of a class. Once you have identified all of its variable/methods, take a second look at each one and ask the following question: Do I want to access this variable/method from other classes in the same package, from derived classes, or from user code? Based on your answer to each part of this question, use the table below to decide which access modifier is most appropriate. External Access public protected default (package) private Same package Yes No Derive class in another package Yes (inheritance only) User code