Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Similar documents
Iteration Abstraction

Type Hierarchy. Lecture 6: OOP, autumn 2003

Testing and Debugging

6.170 Lecture 7 Abstract Data Types MIT EECS

Inheritance (Part 5) Odds and ends

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

Programming II (CS300)

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

Object Oriented Issues in VDM++

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Chapter 5 Object-Oriented Programming

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

Documentation (and midterm review)

Introduction to Programming Using Java (98-388)

Java Object Oriented Design. CSC207 Fall 2014

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

6.170 Recitation #5: Subtypes and Inheritance

EECS2030 Week 7 worksheet Tue Feb 28, 2017

Procedural Abstraction

Chapter 10 Classes Continued. Fundamentals of Java

Objects and Iterators

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

JAVA MOCK TEST JAVA MOCK TEST II

Chapter 4 Defining Classes I

Assertions. Assertions - Example

Advanced JML Erik Poll Radboud University Nijmegen

Interfaces, Polymorphism and Iteration. Lecture 7a: OOP, autumn 2003

Practice for Chapter 11

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Lecture 7: Data Abstractions

OO Design Principles

PROGRAMMING LANGUAGE 2

Programming II (CS300)

CS313D: ADVANCED PROGRAMMING LANGUAGE

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

6.005 Elements of Software Construction Fall 2008

CSC Advanced Object Oriented Programming, Spring Specification

Lecture 4: Procedure Specifications

Inheritance, cont. Notes Chapter 6 and AJ Chapters 7 and 8

15CS45 : OBJECT ORIENTED CONCEPTS

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Java Review: Objects

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Inheritance and Interfaces

CS260 Intro to Java & Android 03.Java Language Basics

Inheritance, Polymorphism, and Interfaces

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Assertions, pre/postconditions

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Super-Classes and sub-classes

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

List ADT. Announcements. The List interface. Implementing the List ADT

CS-202 Introduction to Object Oriented Programming

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance and Polymorphism

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Subclassing for ADTs Implementation

CS 251 Intermediate Programming Inheritance

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

CIS 120 Midterm II November 16, 2012 SOLUTIONS

Lecture 4: Extending Classes. Concept

CO Java SE 8: Fundamentals

CS313D: ADVANCED PROGRAMMING LANGUAGE

Announcements. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Intuition: Type Signature is a Specification.

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Absolute C++ Walter Savitch

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Software Construction

JAVA OBJECT-ORIENTED PROGRAMMING

CS 215 Software Design Sample midterm solutions

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Computer Science II (20073) Week 1: Review and Inheritance

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Programming Languages and Techniques (CIS120)

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Announcements. Representation Invariants and Abstraction Functions. Announcements. Outline

Java Fundamentals (II)

Basic Object-Oriented Concepts. 5-Oct-17

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

1 Shyam sir JAVA Notes

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Lecture 5: Inheritance

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

C++ Important Questions with Answers

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Inheritance. OOP: Inheritance 1

Transcription:

Type Hierarchy Comp-303 : Programming Techniques Lecture 9 Alexandre Denault Computer Science McGill University Winter 2004 February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 1

Last lecture... Adequacy of collection types requires a way to iterate efficiently and conveniently over its elements. Iterators provide a that solution. A generator object returns elements from the collection one at a time, usually without requiring extra storage or requiring access to all elements. Iterators support abstraction by hiding how elements are produced: the generator has access to private variables of the collection but shields the user from this knowledge Iterators assume that the collection remains unchanged while iterating, except through the optional remove() operation February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 2

Announcements... Deliverables today. Req&Spec Document on my desk. Assignment 1 (paper copy) in the McConnell drop off box (first floor) Assignment 1 (electronic copy) on Web CT Assignment 2 will be handed out in 7 days. You should start coding the project (2 months left). February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 3

February... Lecture 9: Type hierarchy Lecture 10: Polymorphic abstractions Lecture 11: Threading Lecture 12: Network, sockets and serialization Lecture 13: Testing, debugging and review Midterm 1 February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 4

Type hierarchy A type family is defined by a type hierarchy. At the top of the hierarchy is a supertype that defines behavior common to all family members. Other members are subtypes of this supertype. A hierarchy can have many levels. Type hierarchy can be used to define multiple implementations of a type that are more efficient under particular circumstances. DensePoly & SparsePoly implement Poly to extend the behavior of a simple type by providing extra methods BufferedReader extends Reader February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 5

Substitution principle A supertypes behavior must be supported by all subtypes. Therefore, in any situation in which a supertype can be used, it can be substituted by a subtype. Java compiler enforces this by only allowing extensions to a type (you can only redefine and add methods, not remove them). The substitution principle provides abstraction by specification for type hierarchies: Subtypes behave in accordance with the specification in their supertype. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 6

Assignment If S is a subtype of T, S objects can be assigned to variables of type T. Poly p1 = new DensePoly(); // the zero Poly Poly p2 = new SparsePoly(3,20); // the Poly 3x^20 p1 has apparent type Poly However, p1, after assignment, has actual type DensePoly. The Java compiler checks types based on apparent type. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 7

Method Dispatch in function languages At compile time, the destination method is known. At compile time, a method call is translated into a jump/branch command to a pre-calculated address. We can t do that in Java. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 8

Dispatching The compiler cannot determine the actual type of p. static Poly diff (Poly p) { // differentiates p Iterator g = p.terms();... } Therefore the compiler cannot know which method is executed for p.terms() (assuming DensePoly and SparsePoly have different implementations for terms()) The choice of method to call is determined at run time by dispatching the call. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 9

Dispatching (cont.) Object P: DensePoly Dispatch Vector DensePoly Dispatch Vector SparsePoly Object Q : SparsePoly Dispatch Vector DP.degree SP.degree Dispatch Vector DP.coef SP.coef Instance Variables Poly.add Instance Variables DP.terms SP.terms Dispatching maps an method call to the actual type s implementation for that call. Each object has a reference to a dispatch vector which stores the implementation for each method. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 10

Method Dispatch in Java 1. Extract the method name and signature from the method call. 2. Search the dispatch vector of the stack object to find our target memory address. 3. Use the method signature to remove arguments from the stack and transfer them to the method s local variables. 4. Transfer control to target memory address. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 11

Method declarations Methods can be declared final A final method cannot be re-defined (overridden) by a subtype. This guaranties that the behavior of the method is frozen. Methods can be declared abstract An abstract method must be overridden by a subtype, otherwise the subtype is also an abstract class. Abstract methods are declared but not implemented. This guaranties that subtypes define their own implementation. Methods and instance variables can be declared protected A protected method is invisible to users (private), but visible to subtypes (public) and classes in the same package. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 12

When to use protected Protected methods & instance variables expose the implementation of a supertype to its subtypes. A subtype can break the implementation of a supertype. The supertype can not be re-implemented without affecting the subtype. Protected should only be used when a subtype needs to access parts of its supertype for efficiency reasons. By default, a subtype should access its supertype only through the public interface. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 13

Specification of IntSet public class IntSet { // OVERVIEW: IntSets are mutable, unbounded sets of integers // A typical IntSet is {x1,...,xn} // constructors public IntSet () // EFFECTS: Initializes this to be empty // methods public void insert (int x) // MODIFIES: this // EFFECTS: Adds x to the elements of this, // i.e. this_post = this + {x} public void remove (int x) // MODIFIES: this // EFFECTS: Removes x from this, i.e. this_post = this - {x} February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 14

Specification of IntSet (cont.) // observers public boolean isin (int x) // EFFECTS: if x is in this returns true else returns false public int size ( ) // EFFECTS: Returns the cardinality of this public int choose ( ) throws EmptyException // EFFECTS: if this is empty, throws EmptyException else // returns an arbitrary element of this public void Iterator elements () // EFFECTS: Returns a generator that produces all elements of this // (as Integers), each exactly once, in arbitrary order // REQUIRES: this not to be modified while the generator is in use public boolean subset (IntSet s) // EFFECTS: Returns true is this is a subset of s else returns false } public String tostring ( ) February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 15

Partial Implementation of IntSet private Vector els; // the elements public boolean subset (IntSet s) { // EFFECTS: Returns true is this is a subset of s else returns false } if (s == null) return false; for (int i = 0; i < els.size ( ); i++) if (!s.isin(((integer) els.get ( i )). intvalue ( ))) return false; return true; If Vector els was declared protected, we could retrieve integer directly. We would not need to call isin multiple times. Unfortunately, subset would only function with IntSet s that uses Vector els. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 16

Subset/Superset Game I will give you two sets of number. You have to tell me if set B is a subset of set A. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 17

First try Set A: 4, 19, 10, 500, 38, 32, 203, 401, 134, 3, 54, 27, 76, 348, 122, 453, 88, 95, 499, 176, 365, 9, 473, 112, 62, 201, 465, 333, 67, 262 Set B: 465, 401, 348, 88, 122, 95, 176, 262, 10, 134, 27, 9, 67 February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 18

Second try Set A: 1, 3, 4, 9, 10, 19, 26, 32, 38, 53, 62, 67, 76, 88, 99, 112, 142, 154, 186, 201, 213, 262, 333, 358, 365, 401, 453, 455, 476, 499, 500 Set B: 9, 10, 67, 99, 201, 333, 365, 401, 453, 477, 499, 500 February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 19

SortedIntSet Suppose we define a type SortedIntSet, which is like an IntSet but gives access to its elements in sorted order by generator returned by terms(). The method subset() could have a more efficient implementation, since we can assume elements are stored in sorted order. We overload subset: public boolean subset (IntSet s) // inherited public boolean subset (SortedIntSet s) // overloaded // same specification but more efficient: // go through elements of this and s // from small to large February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 20

Implementing SortedIntSet To implement SortedIntSet we could use a sorted list. This could make SortedIntSet much more efficient. If SortedIntSet is a subclass of IntSet, every SortedIntSet object will have all instance variables declared for IntSet. However, we do not need a Vector els variable in our SortedIntSet. This means we might need to rethink the structure of our classes. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 21

Abstract IntSet IntSet SortedIntSet UnsortedIntSet IntSet is now an abstract class. Methods common to all IntSets are store in IntSet. Implementation details specific to particular type of IntSet are store in the subtypes. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 22

Abstract IntSet (cont.) public abstract class IntSet { protected int sz; // the size public IntSet () { sz = 0; } public abstract void insert (int x); public abstract void remove (int x); public abstract Iterator elements ( ); public boolean isin (int x) { Iterator g = elements ( ); Integer z = new Integer (x); while (g.hasnext ( )) if (g.next ( ). equals (z )) return true; return false; } } public int size () { return sz; } February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 23

Abstract IntSet (cont.) Instance variable sz is provided to enable all subclasses to implement size ( ) efficiently. The variable sz is declared protected so subclasses can access (read/write) it. The methods insert(), remove() and elements() are abstract. The methods isin(), subset() and tostring() are implemented using abstract methods. These methods are called template since they implement behavior in an abstract class using calls to abstract methods. The object IntSet() can not be called by user since this is an abstract class (no instances). However, subclasses can call the constructor using the super() keyword. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 24

Interfaces An abstract class defines a type and provides a partial implementation such as instance variables and template methods. An interface defines only a type. In other words, it is composed of public nonstatic abstract methods. We can use interfaces with the keyword implements in the header of the class. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 25

Iterator interface public interface Iterator { public boolean hasnext ( ); // EFFECTS: Returns true if there are no more // elements to yield else returns false } public Object next ( ) throws NoSuchElementException; // MODIFIES: this // EFFECTS: If there are more results to yield, returns // the next result and modifies the state of this to // to record the yield. // Otherwise, throws NoSuchElementException February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 26

Multiple Interfaces Interfaces are used when all methods are abstract. Interfaces can be used when a type has multiple supertypes. For example, an object can implement an iterator and a cloneable interface. You can mix both inheritance and interfaces. For example, an object can implement a cloneable interface and extend IntSet. You can only extend one class, but implement multiple interfaces. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 27

Multiple Implementations Type hierarchy can be used to provide multiple implementations of a type. Subclasses only override methods defined in the (abstract) superclass. They do not add public methods (private helper methods are allowed). Constructors must be defined and could be overloaded with extra parameters. User code is defined only in terms of the supertype, except when creating objects. The user is unaware whether a Poly is a DensePoly or a SparsePoly The actual type can change behind the users back. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 28

Meaning of subtypes The substitution principle requires that the subtype specification supports reasoning based on the supertype spec. Three properties must hold: Signature rule: the subtype must have all methods of the supertype, and the signatures of subtype methods must be compatible with the signatures of supertype methods. Methods rule: calls of subtype methods must behave like calls to corresponding supertype methods. Properties rule: the subtype must preserve all properties that can be proven about supertype objects. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 29

Signature rule The signature rule is checked by the Java compiler: The subtype must have all supertype methods with identical signatures. However, a subtype method can have fewer exceptions. Code written in terms of supertype can handle exceptions listed in supertype methods but will work in a type-correct manner if those exceptions are not thrown. Java s compatibility is stricter than necessary. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 30

Methods rule Behavior can not be checked by the compiler. The programmer must guarantee that subtype methods behave like supertype methods. With any IntSet object, the method insert(x) should add a number to the set. In all our examples, subtype methods behave like supertype methods, except for the elements() method of SortedIntSet. The method elements() is underdetermined in our supertype. Requiring elements() to return a generator that gives elements in sorted order narrows the specification but is still correct. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 31

Allowable changes in specs A subtype method can have weaker preconditions and stronger postconditions. Precondition rule: the set of possible input for the subtype if bigger or equal than the set of possible input for the supertype Postcondition rule: the set of possible output for the subtype if smaller or equal than the set of possible output for the supertype February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 32

Allowable changes in specs (cont.) Weakening precondition allows subtype to require less from its caller than the supertype. It allows code written in terms of supertype to execute correctly. Strengthening postcondition allows subtype to restrict the result more than supertype It allows code written in terms of the supertype to execute correctly, since postcondition of supertype follows from postcondition of the subtype. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 33

The properties rule Properties defined for supertype must hold for subtype. The invariant properties always hold: if supertype is immutable, subtype must be immutable. Some related objects cannot be subtyped because of their properties. For example, a SimpleSet is a IntSet which can only grow. IntSet cannot be a subtype of SimpleSet since it has a remove() method and can therefore shrink. SimpleSet cannot be a subtype of IntSet since the remove() method can only be inherited or overridden with preservation of the methods rule. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 34

Summary Type hierarchies improve the structure of programs. By grouping types into a family, the programmer makes clear that there is a relationship between them. This makes code easier to understand than when all types are defined in a flat structure. Hierarchy allows definition of abstractions that work for an entire family. This allows user code that works for all Windows, for example, regardless of the specific type of Window that is passed as argument. Hierarchy provides extensibility. New types can be defined and will be handled correctly by user code that was written even before the new types were invented. February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 35