Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

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

Java Object Oriented Design. CSC207 Fall 2014

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Object Oriented Programming. Java-Lecture 11 Polymorphism

Derived and abstract data types. TDT4205 Lecture 15

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

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

CS558 Programming Languages

Chapter 5 Object-Oriented Programming

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

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

Chapter 14 Abstract Classes and Interfaces

More on Inheritance. Interfaces & Abstract Classes

Module Mechanisms CS412/413. Modules + abstract types. Abstract types. Multiple Implementations. How to type-check?

Data Abstraction. Hwansoo Han

CS-202 Introduction to Object Oriented Programming

Java: introduction to object-oriented features

Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Object-Oriented Programming More Inheritance

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

PROGRAMMING LANGUAGE 2

Inheritance and Polymorphism

G Programming Languages - Fall 2012

CS111: PROGRAMMING LANGUAGE II

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

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

What are the characteristics of Object Oriented programming language?

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

INSTRUCTIONS TO CANDIDATES

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

CS250 Final Review Questions

Method Resolution Approaches. Dynamic Dispatch

Practice for Chapter 11

ECE 122. Engineering Problem Solving with Java

CS250 Final Review Questions

CS 251 Intermediate Programming Inheritance

Java Fundamentals (II)

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Inheritance and object compatibility

COMP322 - Introduction to C++

Lecture 2: Java & Javadoc

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Chapter 10 Classes Continued. Fundamentals of Java

CS260 Intro to Java & Android 03.Java Language Basics

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

JAVA. Lab-9 : Inheritance

Arrays Classes & Methods, Inheritance

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

Lecture Notes on Programming Languages

CS 11 java track: lecture 3

1 Shyam sir JAVA Notes

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

Programming Languages

ob-ject: to feel distaste for something Webster's Dictionary

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

25. Generic Programming

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Data Structures (list, dictionary, tuples, sets, strings)

index.pdf January 21,

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

JAVA MOCK TEST JAVA MOCK TEST II

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

A Short Summary of Javali

ITI Introduction to Computing II

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division DO NOT. P. N.

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

Objects. Prof. Clarkson Fall ob-ject: to feel distaste for something Webster's Dictionary. Today s music: Kung Fu Fighting by CeeLo Green

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

ITI Introduction to Computing II

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Self-review Questions

IC Language Specification

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSE 307: Principles of Programming Languages

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

ITI Introduction to Computing II

Lecture 18 CSE11 Fall 2013 Inheritance

CS111: PROGRAMMING LANGUAGE II

C++ Important Questions with Answers

CMSC 132: Object-Oriented Programming II

Compiler construction 2009

Interfaces, Mixins, & Multiple Inheritance

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

Java Programming Lecture 7

ITI Introduction to Computing II

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

More on Objects in JAVA TM

Unified Modeling Language (UML) Class Diagram

Exercise: Singleton 1

Transcription:

Records CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 15: Objects 25 Feb 05 Objects combine features of records and abstract data types Records = aggregate data structures Combine several iables o a higher-level structure Type is essentially Cartesian product of element types Need selection operator to access fields Pascal records, C structures : struct { float f; char a,b,c; y A; Type:{ float f; char a,b,c; y Selection: A.x = 1; n = A.y; CS 412/413 Spring 2005 Introduction to Compilers 1 CS 412/413 Spring 2005 Introduction to Compilers 2 Objects have fields in addition, they have methods = procedures that manipulate the data (fields) in the object Hence, objects combine data and computation Objects as Records class List List { len; Cell Cell head, tail; length(); List List append( d); d); List List rest(); ADTs Abstract Data Types (ADT): separate from Specification: provide an abstract type for data Implementation: must match abstract type : linked list Cell Cell = { data; Cell Cell next; List List = { {len; Cell Cell head, tail; tail; length(); length() { return l.len; List List append ( (d); first() { return head.data; List List rest() { return head.next; List List rest(); List List append( d) d) { CS 412/413 Spring 2005 Introduction to Compilers 3 CS 412/413 Spring 2005 Introduction to Compilers 4 Objects as ADTs Specification: public methods and fields of the object Implementation: Source code for a class defines the concrete type () class List List { private len; private Cell head, tail; public static length() {; public static List List append( d) d) {; public static first() { ;; public static List List rest() {; Objects What objects are: Aggregate structures that combine data (fields) with computation (methods) Fields have public/private qualifiers (can model ADTs) Need special support in many compilation stages: Type checking Static analysis and optimizations Implementation, run-time support Features: inheritance, subclassing, polymorphism, subtyping, overriding, dynamic dispatch, abstract classes, erfaces, etc. CS 412/413 Spring 2005 Introduction to Compilers 5 CS 412/413 Spring 2005 Introduction to Compilers 6

Inheritance Inheritance = mechanism that exposes common features of different objects Class B extends class A = B has the features of A, plus some additional ones, i.e., B inherits the features of A B is subclass of A; and A is superclass of B class Po { float x, x, y; y; float getx(){ ; ; float gety(){ ; ; class ColoredPo extends Po { color; getcolor(){ ; ; CS 412/413 Spring 2005 Introduction to Compilers 7 Single vs. Multiple Inheritance Single inheritance: inherit from at most one other object (Java) Multiple inheritance: may inherit from multiple objects (C++) class A A{ { class B B{ { a; a; b; b; geta(){; getb(){; class C :: A, A, B { c; c; getc(){; CS 412/413 Spring 2005 Introduction to Compilers 8 Inheritance and Scopes How do objects access fields and methods of: Their own? Their superclasses? Other unrelated objects? Each class declaration roduces a scope Contains declared fields and methods Scopes of methods are sub-scopes Inheritance implies a hierarchy of class scopes If B extends A, then scope of A is a parent scope for B v; class B extends A { bool y; t; A o; z; y t x f fun bool z v arg o z A CS 412/413 Spring 2005 Introduction to Compilers 9 CS 412/413 Spring 2005 Introduction to Compilers 10 Class Scopes Resolve an identifier occurrence in a method: Look for symbols starting with the symbol table of the current block in that method Resolve qualified accesses: Accesses o.f, where o is an object of class A Walk the symbol table hierarchy starting with the symbol table of class A and look for identifier f Special keyword this refers to the current object, start with the symbol table of the enclosing class Class Scopes Multiple inheritance: A class scope has multiple parent scopes Which should we search first? Problem: may find symbol in both parent scopes! Overriding fields: Fields defined in a class and in a subclass Inner declaration shadows outer declaration Symbol present in multiple scopes CS 412/413 Spring 2005 Introduction to Compilers 11 CS 412/413 Spring 2005 Introduction to Compilers 12

Inheritance and Typing Classes have types Type is Cartesian product of field and method types Type name is the class name What is the relation between types of parent and inherited objects? Subtype Subset A value of type S may be used wherever a value of type T is expected S <: T values(s) values(t) Subtyping: if class B extends A then Type B is a subtype of A Type A is a supertype B class A B extends A values of type S values of type T Notation: B <: A CS 412/413 Spring 2005 Introduction to Compilers 13 CS 412/413 Spring 2005 Introduction to Compilers 14 Subtype Properties If type S is a subtype of type T (S <: T), then: a value of type S may be used wherever a value of type T is expected (e.g., assignment to a iable, passed as argument, returned from method) Po ColoredPo y; x = y; ColoredPo <: Po subtype supertype Polymorphism: a value is usable as several types Subtype polymorphism: code using T s can also use S s; S objects can be used as S s or T s. CS 412/413 Spring 2005 Introduction to Compilers 15 Implications of Subtyping We don t statically know the types of object references Can be the declared class or any subclass Precise types of objects known only at run-time Problem: overriden fields / methods Declared in multiple classes in hierarchy. Don t know statically which declaration to use at compile time Java solution: statically resolve fields using declared type of reference; no field overriding dynamically resolve methods using the object s type (dynamic dispatch); require identical signatures for all overridden methods to support static type checking CS 412/413 Spring 2005 Introduction to Compilers 16 v; bool y; g( z) { w; y bool z arg g fun v g symtab a.x z arg b.y... w CS 412/413 Spring 2005 Introduction to Compilers 17 v; bool x; w; x bool z arg v a.x z arg b.x... w CS 412/413 Spring 2005 Introduction to Compilers 18

v; bool x; w; x bool z arg v a.f(1) z arg b.f(1)... w CS 412/413 Spring 2005 Introduction to Compilers 19 Objects and Typing Objects have types but also have code for methods ADT perspective: Specification = typing Implementation = method code, private fields Objects mix with Can we separate types from? CS 412/413 Spring 2005 Introduction to Compilers 20 Interfaces Interfaces are pure types; they don t give any class classmylist MyListimplements List List {{ private len; private Cell Cell head, head, tail; tail; public public length() {; {; public publiclist List append( d) d) {; {; public public first() { {;; public publiclist List rest() rest() {; {; erface List List {{ length(); List List append( d); d); List List rest(); rest(); CS 412/413 Spring 2005 Introduction to Compilers 21 Multiple Implementations Interfaces allow multiple s erface List { length(); List append(); first(); List rest(); class LenList implements List { private len; private Cell head, tail; private LenList() { public List append( d) { public length() { return len; class SimpleList implements List { private data; private SimpleList next; public length() { return 1+next.length() CS 412/413 Spring 2005 Introduction to Compilers 22 Implementations of Multiple Interfaces erface A { foo(); erface B { bar(); class AB implements A, B { foo(){ bar(){ Subtyping vs. Subclassing Can use inheritance for erfaces Build a hierarchy of erfaces erface A { erface B extends A { Objects can implement erfaces class C implements A { Subtyping: erface inheritance Subclassing: object (class) inheritance Subclassing implies subtyping B <: A C <: A CS 412/413 Spring 2005 Introduction to Compilers 23 CS 412/413 Spring 2005 Introduction to Compilers 24

Abstract Classes Subtypes in Java Classes define types and some values (methods) Interfaces are pure object types Abstract classes are halfway: define some methods leave others unimplemented no objects (instances) of abstract class erface I 1 extends I 2 { I 2 I 1 class C implements I { I C class C 1 extends C 2 C 2 C 1 I 1 <: I 2 C <: I C 1 <: C 2 CS 412/413 Spring 2005 Introduction to Compilers 25 CS 412/413 Spring 2005 Introduction to Compilers 26 Subtyping Properties Subtype relation is reflexive: T <: T Transitive: R <: S and S <: T implies R <: T Anti-symmetric: T 1 <: T 2 & T 2 <: T 1 T 1 = T 2 Defines a partial ordering on types! Use diagrams to describe typing relations Subtype Hierarchy Introduction of subtype relation creates a hierarchy of types: subtype hierarchy type or subtype hierarchy C1 I1 I2 C2 C3 C4 C5 I3 class/inheritance hierarchy CS 412/413 Spring 2005 Introduction to Compilers 27 CS 412/413 Spring 2005 Introduction to Compilers 28