Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Similar documents
Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

The Java Type System (continued)

Section: Inheritance Heck

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Passing Out Review Forms

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CSc 335 Inheritance Hell

8 Understanding Subtyping

Rules and syntax for inheritance. The boring stuff

CSE341, Fall 2011, Lecture 26 Summary

Programming Languages and Techniques (CIS120)

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

16 Multiple Inheritance and Extending ADTs

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

CLASS DESIGN. Objectives MODULE 4

Object-Oriented Design Lecture 7 CSU 370 Fall 2008 (Pucella) Friday, Oct 3, 2008

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

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CS1004: Intro to CS in Java, Spring 2005

Subclassing for ADTs Implementation

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

type conversion polymorphism (intro only) Class class

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

ITI Introduction to Computing II

Polymorphism. return a.doublevalue() + b.doublevalue();

ITI Introduction to Computing II

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Object-Oriented Concepts

Equality for Abstract Data Types

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

Inheritance. Transitivity

Concepts of Programming Languages

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

Scoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values.

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Operational Semantics of Cool

Overview. Elements of Programming Languages. Objects. Self-Reference

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

index.pdf January 21,

Distributed Systems Recitation 1. Tamim Jabban

CS 251 Intermediate Programming Inheritance

CSE 143 Lecture 22. The Object class; Polymorphism. read slides created by Marty Stepp and Ethan Apter

Programming Languages and Techniques (CIS120)

type environment updated subtype sound

ECE 122. Engineering Problem Solving with Java

Operational Semantics. One-Slide Summary. Lecture Outline

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

CS 10, Fall 2015, Professor Prasad Jayanti

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Programming Exercise 14: Inheritance and Polymorphism

Compilers. Cool Semantics II. Alex Aiken

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof

CS111: PROGRAMMING LANGUAGE II

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Operational Semantics of Cool

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Code Reuse: Inheritance

Computational Expression

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CSE 143 Lecture 12 Inheritance

Computer Science 1 Honors

Programming Languages 2nd edition Tucker and Noonan"

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

AP CS Unit 6: Inheritance Notes

First Java Program - Output to the Screen

University of Palestine. Mid Exam Total Grade: 100

15 Multiple Inheritance and Traits

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

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

CSCI 1103: Object-Oriented Objects

Implements vs. Extends When Defining a Class

CS558 Programming Languages

Java Magistère BFA

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Interfaces. Java interface. Notes. Example of code duplication. Why an interface construct? Interfaces & Types. good software engineering

Bucket Sort Puzzlers. 08 A: Sorting IV. CS1102S: Data Structures and Algorithms. Martin Henz. March 12, 2010

CS61B Lecture #8: Object-Oriented Mechanisms

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Overview of Semantic Analysis. Lecture 9

Operational Semantics

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Polymorphism. Agenda

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Transcription:

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007 (These notes are very rough, and differ somewhat from what I presented in class; I am posting them here to supplemental your own notes.) 1 Type Checking Java Before the midterm, we started looking at generics, that is, a way to parameterize interfaces and classes by types. Last lecture, I would have presented the Java Collections framework, which are a bunch of classes and interfaces distributed in Java that implement sets, and lists, using generic interfaces. The reading is on the web page, and I will ask you to read it carefully, because it essentially counts as a lecture. Okay, so we have generics. Now the type system has become a bit more complicated. Let s revisit the type system and look carefully at what it guarantees, and how Java establishes that guarantees. Type checking happens before execution. Type checking can only uses type information that you have declared in your program, and not the possible type that an object has at runtime. To type check, Java looks at every statement in the code, and make sure that it uses values correctly, according to the declared types. These checks are done using a set of rules, which are called type checking rules. A rule takes the form "if A is true, then B is true"; to simplify things (it doesn t look like a simplification, but it turns out it does), we will write such a rule as: A is true --------- B is true During type checking, we carry around an environment (called E) that records the the type of the various symbols (variables, class names) that are currently in scope. The initial environment Einit holds the type of all the classes defined in the Java library. 1

Here are the rules. Let s start at the high-level, type checking a program. A program type checks in environment E (written E - P ok) if all the classes in the program type check. (Recall that a program is just a sequence of classes.) A class type C checks in environment E (written E - C ok) if all the fields and methods it contains type check. Thus, to type check a program, we type check every class in the program, assuming that all the classes in the program are added to the environment. (There is a circularity here, but it turns out not to be a problem.) To type check a class, we need to type checvk all the methods in the class, as well as all the fields. Let s focus on the methods for now. A method type checks (written E - T name (T1 n1,..., Tk nk) {... } ok) if all the statements and declarations it contains type check. A simple form (without declaration) might be: E+{n1:T1,...,nk:Tk} - s1 ok... E+{n1:T1,...,nk:Tk} - sn ok ----------------------------------------------------- E - T name (T1 n1,..., Tk nk) { s1;...; sn; } ok (Declarations just add new bindings to the environment) Rules for type checking statements (E - s ok) and expressions that evaluate to a value (E - ex : T) Sample statements: assignments (var = ex;) void expressions (ex;), and, say, conditionals (if (e) s else s;) E - var : T E - ex : T ------------------------------------------- E - var = ex ok E - ex : void --------------------- E - ex ok E - ex : boolean E - s1 ok E - s2 ok E - if (ex) s1 else s2 2

How about expressions? Constants are easy: ----------------- E - 1 : int (and so on for all integer constants) The main rule is type application: E - obj : Class {...T name (T1,...,Tn)...} E - e1 : T1... E - en : Tn ------------------------------------------------- E - obj.name (e1,...,en) : T This says that the type system derives that obj.name (e1,...,en) has type T when e1,..., en have type T1,..., Tn respectively, and obj has a method "name" of the right type. So, when type checking, say, System.out.println (x.tostring()), the environment has name Foo in it (with type indicating it is a class with methods void sample(object) and void test()), and also name System which is a class with field out holding an object with a method println expecting a String, and also x with type Object (according to the declaration of the parameters). Now, x has type Object, so it has a method called tostring(), so x.tostring() is okay, and the result is a string, so that the call to println is also okay, and returns no value (type void). So the statement type checks. So in particular, instantiating the above rule to the case above. E - System.out : PrintStream {...void println (String)...} E - x.tostring() : String E - System.out.println (x.tostring()) : void The above is fine, but we need to establish that x.tostring() has type String. Well, this is again established using the above rule: 3

E - x : Object (by looking in E) {...String tostring()...} E - x.tostring () : String Actually, the real rules are slightly more general: E - obj : Class {...T name (T1,...,Tn)...} E - e1 : U1... E - en : Un E - U1 <= T1... E - Un <= Tn ------------------------------------------------- E - obj.name (e1,...,en) : T E - var : T E - ex : U E - U <= T --------------------------------------------------- E - var = ex ok where E - U <= T means: if U is a subtype of T in environment E (which defines, for instance, what subclasses are subclasses of what classes) ------------- E - U <= T if E says that U <= T (e.g. U = class that implements T) ------------- E - T <= T E - U <= T ----------------------- E - U[] <= T[] E - U <= T E - T <= S ----------------------------------- E - U <= S This more general rule is used to type check: this.sample (i); in the second method. 4

E - this : Foo {...void sample (Object)...} E - i : Integer (from E) E - Integer <= Object (from E) --------------------------------------------- E - this.sample(i) : void So there is a well defined notion of what it means for a program to type check. What does type checking guarantees? It says that some bad things cannot happen at runtime. At runtime, every object created has an associated run-time class (the class it was created as). Operationally, at run-time, when an object is passed to a method that may expect a superclass, the extra methods are not deleted, they are still there, although the method may not be able to access them (because type checking ensured that the system cannot look at them). The main runtime guarantees: if Einit - P ok, then during execution of the program, whenever the system attempts to invoke method M on some object O, then object O implements method M. (In older OO languages that did not have static type checking, we could have an exception "Method undefined". We never get this for Java. The above is often called soundness.) 5