CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012



Similar documents
Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Programming Languages Featherweight Java David Walker

Inheritance, overloading and overriding

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Stack Allocation. Run-Time Data Structures. Static Structures

Moving from CS 61A Scheme to CS 61B Java

Lecture 9. Semantic Analysis Scoping and Symbol Table

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Implementation Aspects of OO-Languages

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Pluggable Type Systems. Gilad Bracha

Type Classes with Functional Dependencies

1. Polymorphism in C++...2

MCI-Java: A Modified Java Virtual Machine Approach to Multiple Code Inheritance

Data Abstraction and Hierarchy

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Python, C++ and SWIG

Java Interview Questions and Answers

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Polymorphism. Why use polymorphism Upcast revisited (and downcast) Static and dynamic type Dynamic binding. Polymorphism.

Type-Safe Feature-Oriented Product Lines

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

CS 111 Classes I 1. Software Organization View to this point:

Description of Class Mutation Mutation Operators for Java

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

On Understanding Types, Data Abstraction, and Polymorphism

Habanero Extreme Scale Software Research Project

On Understanding Types, Data Abstraction, and Polymorphism

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Checking Access to Protected Members in the Java Virtual Machine

Compile-time type versus run-time type. Consider the parameter to this function:

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

An Incomplete C++ Primer. University of Wyoming MA 5310

Java Application Developer Certificate Program Competencies

Integrating Typed and Untyped Code in a Scripting Language

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CIS 190: C/C++ Programming. Polymorphism

Object Instance Profiling

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

Crash Course in Java

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

The Design of the Inferno Virtual Machine. Introduction

Android Application Development Course Program

Extending Classes with Services

Java (12 Weeks) Introduction to Java Programming Language

Java Programming Language

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Basic Object-Oriented Programming in Java

Semantic Analysis: Types and Type Checking

Roulette: Inheritance Case Study

Object Oriented Design

CSE 326: Data Structures. Java Generics & JUnit. Section notes, 4/10/08 slides by Hal Perkins

Lecture 1 Introduction to Android

Integrating Formal Models into the Programming Languages Course

Abstract. a

Fruit Machine. Level. Activity Checklist Follow these INSTRUCTIONS one by one. Test Your Project Click on the green flag to TEST your code

Decision Making under Uncertainty

Pitfalls of Object Oriented Programming

Programming Project 1: Lexical Analyzer (Scanner)

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

16.1 DataFlavor DataFlavor Methods. Variables

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

The Cool Reference Manual

Génie Logiciel et Gestion de Projets. Object-Oriented Programming An introduction to Java

Java EE Web Development Course Program

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Philosophical argument

Part VI. Object-relational Data Models

PLV Goldstein 315, Tuesdays and Thursdays, 6:00PM-7:50PM. Tuesdays and Thursdays, 4:00PM-5:30PM and 7:50PM 9:30PM at PLV G320

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Two-State Options. John Norstad. January 12, 1999 Updated: November 3, 2011.

Relative and Absolute Change Percentages

The Separation of Interface and Implementation in C++

Getting started as a self investor. Your guide to self investing

Programming with Java GUI components

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

Transcription:

CS-XXX: Graduate Programming Languages Lecture 25 Multiple Inheritance and Interfaces Dan Grossman 2012

Multiple Inheritance Why not allow class C extends C1,C2,...{...} (and C C1 and C C2)? What everyone agrees: C++ has it and Java doesn t All we ll do: Understand some basic problems it introduces and how interfaces get most of the benefits and some of the problems Problem sources: Class hierarchy is a dag, not a tree (not true with interfaces) Subtype hierarchy is a dag, not a tree (true with interfaces) Dan Grossman CS-XXX 2012, Lecture 25 2

Diamond Issues If C extends C1 and C2 and C1,C2 have a common superclass D (perhaps transitively), our class hierarchy has a diamond If D has a field f, should C have one field f or two? If D has a method m, C1 and C2 will have a clash If subsumption is coercive (changing method-lookup), how we subsume from C to D affects run-time behavior (incoherent) Diamonds are common, largely because of types like Object with methods like equals Dan Grossman CS-XXX 2012, Lecture 25 3

Multiple Inheritance, Method-Name Clash If C extends C1 and C2, which both define a method m, what does C mean? Possibilities: 1. Reject declaration of C (Too restrictive with diamonds) 2. Require C to override m (Possibly with directed resends) 3. Left-side (C1) wins (Must decide if upcast to right-side (C2) coerces to use C2 s m or not) 4. C gets both methods (Now upcasts definitely coercive and with diamonds we lose coherence) 5. Other? Dan Grossman CS-XXX 2012, Lecture 25 4

Implementation Issues This isn t an implementation course, but many semantic issues regarding multiple inheritance have been heavily influenced by clever implementations In particular, accessing members of self via compile-time offsets...... which won t work with multiple inheritance unless upcasts adjust the self pointer That s one reason C++ has different kinds of casts Better to think semantically first (how should subsumption affect the behavior of method-lookup) and implementation-wise second (what can I optimize based on the class/type hierarchy) Dan Grossman CS-XXX 2012, Lecture 25 5

Digression: Casts A cast can mean many things (cf. C++). At the language level: upcast: no run-time effect until we get to static overloading downcast: run-time failure or no-effect conversion: key question is round-tripping reinterpret bits : not well-defined At the implementation level: upcast: usually no run-time effect but see last slide downcast: usually only run-time effect is failure, but... conversion: same as at language level reinterpret bits : no effect (by definition) Dan Grossman CS-XXX 2012, Lecture 25 6

Least Supertypes Consider if e 1 then e 2 else e 3 (or in C++/Java, e 1? e 2 : e 3 ) We know e 2 and e 3 must have the same type With subtyping, they just need a common supertype Should pick the least (most-specific) type Single inheritance: the closest common ancestor in the class-hierarchy tree Multiple inheritance: there may be no least common supertype Example: C1 extends D1, D2 and C2 extends D1, D2 Solutions: Reject (i.e., programmer must insert explicit casts to pick a common supertype) Dan Grossman CS-XXX 2012, Lecture 25 7

Multiple Inheritance Summary Method clashes (what does inheriting m mean) Diamond issues (coherence issues, shared (?) fields) Implementation issues (slower method-lookup) Least supertypes (may be ambiguous) Complicated constructs lead to difficult language design Doesn t necessarily mean they are bad ideas Now discuss interfaces and see how (and how not) multiple interfaces are simpler than multiple inheritance... Dan Grossman CS-XXX 2012, Lecture 25 8

Interfaces An interface is just a (named) (object) type. Example: interface I { Int get_x(); Bool compare(i); } A class can implement an interface. Example: class C implements I { Int x; Int get_x() {x} Bool compare(i i) {...} // note argument type } If C implements I, then C I Requiring explicit implements hinders extensibility, but simplifies type-checking (a little) Basically, C implements I if C could extend a class with all abstract methods from I Dan Grossman CS-XXX 2012, Lecture 25 9

Interfaces, continued Subinterfaces (interface J extends I {...}) work exactly as subtyping suggests they should An unnecessary addition to a language with abstract classes and multiple inheritance, but what about single inheritance and multiple interfaces: class C extends D implements I1,I2,...,In Method clashes (no problem, inherit from D) Diamond issues (no problem, no implementation diamond) Implementation issues (still a problem, different object of type I will have different layouts) Least supertypes (still a problem, this is a typing issue) Dan Grossman CS-XXX 2012, Lecture 25 10

Using Interfaces Although it requires more keystrokes and makes efficient implementation harder, it may make sense (be more extensible) to: Use interface types for all fields and variables Don t use constructors directly: For class C implementing I, write: I makei(...) { new C(...) } This is related to factory patterns ; constructors are behind a level of indirection It is using named object-types instead of class-based types Dan Grossman CS-XXX 2012, Lecture 25 11