Visibility, Accessibility, and Information Hiding

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Programming Languages and Paradigms
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 9: Subprogram Control
Lecture 9 Concepts of Programming Languages
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Visibility, Accessibility, and Information Hiding.
Basic Semantics Associating meaning with language entities.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
© 2006 Pearson Addison-Wesley. All rights reserved How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Advanced UML Class Diagrams.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Topics AliasesSubprograms Generics & Configurations.
Object Oriented Analysis and Design Class and Object Diagrams.
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Test 2 Review Outline.
OOP: Encapsulation &Abstraction
Class and Objects UNIT II.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Abstract Data Types and Encapsulation Concepts
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Examples of Classes & Objects
Objects as a programming concept
GRASP: Visibility and Design
How to Design Supplier Classes
Andy Wang Object Oriented Programming in C++ COP 3330
TIM 58 Chapter 8: Class and Method Design
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
CSE 3302 Programming Languages
Chapter 10: Visibility Chapter 18 in Applying UML and Patterns Book.
Built-In (a.k.a. Native) Types in C++
Classes & Objects: Examples
UML Class Diagram.
Engineering Computation with MATLAB
Generic programming in Java
More Object-Oriented Programming
Bryan Burlingame 13 February 2019
Namespaces, Scopes, Access privileges
Programs and Classes A program is made up from classes
Chapter 9: Value-Returning Functions
COP 3330 Object-oriented Programming in C++
Objectives In this chapter, you will:
The Three Attributes of an Identifier
Object-Oriented Design AND CLASS PROPERTIES
Scope Rules.
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects
Presentation transcript:

Visibility, Accessibility, and Information Hiding

Objectives To explain how visibility rules make program entities accessible through their names To show how references and aliases can extend access beyond visibility To emphasize the importance of limiting visibility and not extending access for hiding information To introduce cases where extending access is permissible

Topics Names and visibility References and aliases Accessibility and information hiding Heuristics for hiding information Violating information hiding practices

Entities, Names, and Visibility A program entity is anything in a program that is treated as a unit. A name is an identifier bound to a program entity. A program entity is visible at a point in a program text if it can be referred to by name at that point; the portion of a text over which an entity is visible is its visibility.

Visibility Example File: package1/PublicClass.java package package1; public class PublicClass{ private String privateAttribute; String packageAttribute; public void method() { String localVariable; ... // point A } // point B } // end package1.PublicClass File: package1/PackageClass.java class PackageClass{ // point C } // end package1.PackageClass File: package2/PackageClass.java package package2; import package1.*; // point D } // end package2.PackageClass Visibility Example

Types of Visibility Local—Visible only within the module where it is defined Non-local—Visible outside the module where it is defined, but not visible everywhere in a program Global—Visible everywhere in a program An entity is exported from the module where it is defined if it is visible outside that module.

Object-Oriented Feature Visibility Private—Visible only within the class where it is defined A type of local visibility Package—Visible in the class where it is defined as well as classes in the same package or namespace A form of non-local visibility Protected—Visible in the class where it is defined and all sub-classes Public—Visible anywhere the class is visible A form of non-local or global visibility

Accessibility A program entity is accessible wherever it is visible. A program entity is accessible at a point in a program text if it can be used at that point. A program entity is accessible wherever it is visible. A program entity may also be accessible where it is not visible.

A variable is a programming language device for storing values. Variables A variable is a programming language device for storing values. Variables have attributes: Name Value Address

References A reference is an expression that evaluates to an address where a value is stored.

An alias is a variable with the same address as another variable. Aliases An alias is a variable with the same address as another variable.

Extending Access Beyond Visibility References and aliases can make variables accessible where they are not visible Passing a reference as an argument Passing an argument by reference (aliasing) Returning a reference from a sub-program This practice is extending access beyond visibility Generally it is a bad practice

Information Hiding and Access The key technique for hiding information is to restrict access to program entities as much as possible. Limiting visibility—Use scope and visibility markers to restrict visibility Not extending access—Avoid using references and aliases to extend visibility A defensive copy is a copy of an entity held by reference passed to or returned from another operation.

Information Hiding Heuristics 1 Limit visibility. Make program entities visible in the smallest possible program region. Restrict the scope of declarations to the smallest possible program region. Make attributes at least protected and preferably private. Make helper operations at least protected and preferably private. Avoid global visibility. Avoid package visibility.

Information Hiding Heuristics 2 Don’t extend access. Don’t initialize attributes with references passed to the class—make defensive copies instead. Don’t pass or return references to attributes—pass or return defensive copies instead. Don’t pass parameters by reference. Don’t make aliases

Exceptions Two cases when access may be extended beyond visibility: Modules must share an entity to collaborate Example: a shared queue Some other design goal is of greater importance than information hiding Example: performance constraints

Summary Program entities are usually accessible through their names by being visible in various parts of a program text. Entities may also be accessed through references or aliases. Information hiding dictates that visibility be limited and that access not be extended beyond visibility. Occasionally this rule can be violated to achieve other goals.