Object Encapsulation CSC 422 Dr. Spiegel.

Slides:



Advertisements
Similar presentations
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Advertisements

Fields, Constructors, Methods
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
External Uniqueness Presented by Nir Atias Dave Clarke Tobias Wrigstad Encapsulation Seminar 2006.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Ownership Types for Object Encapsulation Barbara Liskov Chandrasekhar Boyapati Liuba Shrira Laboratory for Computer Science Massachusetts Institute of.
A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University.
Laboratory for Computer Science Massachusetts Institute of Technology Ownership Types for Safe Region-Based Memory Management in Real-Time Java Chandrasekhar.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
A Seminar on Encapsulation Noam Rinetzky Mooly Sagiv Summary.
Ownership Types for Object Encapsulation Authors:Chandrasekhar Boyapati Barbara Liskov Liuba Shrira Presented by: Charles Lin Course: CMSC 631.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Writing Classes (Chapter 4)
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
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,
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Abstraction ADTs, Information Hiding and Encapsulation.
Views of Data Data – nouns of programming world the objects that are manipulated information that is processed Humans like to group information Classes,
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Featherweight Generic Ownership Alex Potanin, James Noble Victoria University of Wellington Dave Clarke CWI, Netherlands Robert Biddle Carlton University.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Generic Programming and Inner classes ge·ner·ic 1a : relating or applied to or descriptive of all members of a genus, species, class, or group : common.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Iteration Abstraction SWE Software Construction Fall 2009.
Object Oriented Programming in Java Habib Rostami Lecture 10.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
Examples (D. Schmidt et al)
Sections 3.4 Formal Specification
CSE691 Software Models and Analysis.
EECE 310: Software Engineering
Abstract Data Types and Encapsulation Concepts
11.1 The Concept of Abstraction
Iteration Abstraction
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
CSC 480 Software Engineering
Encapsulation & Visibility Modifiers
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Abstract Data Types and Encapsulation Concepts
Dynamic Data Structures and Generics
Iteration Abstraction
Design Patterns Difficult to describe abstractly Elements:
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Dynamic Data Structures and Generics
Sylnovie Merchant, Ph.D. MIS 161 Spring 2005
Programming Languages and Paradigms
Software Design Lecture : 39.
CSG2H3 Object Oriented Programming
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

Object Encapsulation CSC 422 Dr. Spiegel

Outline Object Encapsulation Ownership Types

Encapsulation Level O raw lines of code Level 1 the procedural module class/object structure

Higher Levels of Encapsulation Packages and components (groupings of classes with only some of their interfaces visible externally Level -4 Compositions Design trade offs become more complex as encapsulation becomes more complex

~ ~ Object Encapsulation Consider a Set object s implemented using a Vector object v s o ~ ~ v Local reasoning about s is possible: If objects outside s do not access v v is encapsulated within s

Encapsulation In general, all objects that s depends on must be encapsulated within s s depends on x if mutations of x affect behavior of s

Iterators and Encapsulation Iterators require access to representation i s v Okay if violations of encapsulation limited to the same module

Ownership Types Goal is to enforce encapsulation statically v Programmer can declare s owns v System ensures v is encapsulated in s

Ownership Types Every object has an owner world Every object has an owner Owner can be another object or world Who owns main? Ownership relation forms a tree Owner of an object cannot change

Ownership Types for Encapsulation If an object owns objects it depends on the type system enforces encapsulation If v is inside s and o is outside, o cannot access v s o ~ ~ v

Ownership Types world

TStack Example (No Owners) class TStack { TNode head; void push(T value) {…} T pop() {…} } class TNode { TNode next; T value; … class T {…} TStack head value next value next value next TNode T … … … Can Tstack access Tnodes? Can a Tnode access T’s data?

TStack Example (With Owners) class TStackstackOwner, TOwner { TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class T TOwner {…} TStack TNode T Who owns the nodes? Classes are parameterized with owners

TStack Example class TStackstackOwner, TOwner { TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class T TOwner {…} TStack TNode T First owner owns the “this” object

TStack Example class TStackstackOwner, TOwner { TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class T TOwner {…} TStack TNode T TStack owns the “head” TNode

TStack Example class TStackstackOwner, TOwner { TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class T TOwner {…} TStack TNode T The “next” TNode has the same owner as the “this” TNode All TNodes have the same owner

TStack Example class TStackstackOwner, TOwner { TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class ClientclientOwner { TStackthis, this s1; TStackthis, world s2; TStackworld, world s3; Client TStack TNode T s1 is an encapsulated stack with encapsulated elements

TStack Example class TStackstackOwner, TOwner { world TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class ClientclientOwner { TStackthis, this s1; TStackthis, world s2; TStackworld, world s3; world Client TStack TNode T s2 is an encapsulated stack with public elements

TStack Example class TStackstackOwner, TOwner { world TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class ClientclientOwner { TStackthis, this s1; TStackthis, world s2; TStackworld, world s3; world TStack TNode T s3 is a public stack with public elements

TStack Example class TStackstackOwner, TOwner { TNodethis, TOwner head; } class TNodenodeOwner, TOwner { TNodenodeOwner, TOwner next; TTOwner value; class ClientclientOwner { TStackthis, this s1; TStackthis, world s2; TStackworld, world s3; TStackworld, this s4; // illegal TStack TNode T Other owners must be same as or more public than first owner This constraint is necessary to enforce encapsulation with subtyping

Constraints on Owners class ClientcOwner, sOwner, tOwner where (sOwner <= tOwner) { … TStacksOwner, tOwner head; } This is legal only if tOwner is same as or more public than sOwner

Iterators Consider an Iterator i over Stack s If i is encapsulated within s Then i cannot be used outside s If i is not encapsulated within s Then i cannot access representation of s TStack TStackEnum

Solution Use inner classes Gives desired access to representation Also satisfies modularity goal

Iterators TStack TStackEnum class TStackstackOwner, TOwner { TNodethis, TOwner head; …. class TStackEnumenumOwner, TOwner implements TEnumenumOwner, TOwner { TNodeTStack.this, TOwner current = TStack.this.head; } Inner class objects can access rep of outer class objects TStack TStackEnum

Ownership Types for Encapsulation If an object owns objects it depends on, then the type system enforces encapsulation If v is inside s and o is outside o cannot access v, unless o is an inner class object of s s o ~ ~ What must be true of o so we can declare an instance of o? v