CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Language Issues for Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.3 © Mitchell Wand, This work is licensed under a Creative.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CSE341: Programming Languages Lecture 22 Multiple Inheritance, Interfaces, Mixins Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Winter 2014.
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
Chapter 12 Support for Object oriented Programming.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
ISBN Object-Oriented Programming Chapter Chapter
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CSCE 240 – Intro to Software Engineering Lecture 3.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Programming in Java: lecture 7
Advanced Programming in Java
Advanced Programming in Java
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
Inheritance and Polymorphism
CSE 341 Section 9 Nick Mooney Spring 2017
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
Types of Programming Languages
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Zach Tatlock Winter 2018.
CSE341: Programming Languages Ruby: Blocks & Procs; Inheritance & Overriding Alan Borning Spring 2018.
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018.
CSE 341 Section 9 Winter 2018 Adapted from slides by Eric Mullen, Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Spring 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2016.
Advanced Java Programming
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
Object Oriented Programming
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
Java – Inheritance.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Advanced Programming in Java
Nicholas Shahan Spring 2016
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
CSE 341 Section 9 Fall 2017 Adapted from slides by Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2017.
Lecture 10 Concepts of Programming Languages
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1

Overview Essence of object-oriented programming: inheritance, overriding, dynamic-dispatch Classic inheritance includes specification (types) and implementation (code) What about multiple inheritance (>1 superclass)? –When does it make sense? –What are the issues?

Inheritance Models Single Inheritance: at most 1 superclass –Subclass inherits methods and state from superclass; can override methods, add more methods and instance variables Multiple Inheritance: >1 superclass –Why? Factor different traits/behavior into small classes, then extend several of them –But hard to use well (e.g., C++) Typical problem: big, brittle inheritance graph, methods migrate to bloated superclasses over time; becomes (very) hard to make changes

Inheritance Models Java-style interfaces: >1 type –Doesn’t apply to dynamically-typed languages –Class “inherits” (has) multiple types, but… –…only inherits code from one parent class –Fewer problems than multiple inheritance Mixins: >1 “source of methods” –Similarities to multiple inheritance – many of the goodies with fewer(?) problems

Multiple Inheritance If single inheritance is so useful, why not allow multiple superclasses? –Semantics are often awkward (next few slides) –Static type checking is harder (not discussed) –Efficient implementation is harder (hints next time) Is it useful? Sure: –Color3DPoint extends 3DPoint, ColorPoint –StudentAthlete extends Student, Athlete Naïve view: subclass has all fields and methods of all superclasses; avoids copying code

Trees, DAGs, and Diamonds Class hierarchy forms a graph –Nodes are classes –Edges from subclasses to superclasses –Single inheritance: a tree –Multiple inheritance: a DAG (but no cycles allowed) Diamonds –With multiple inheritance, may be multiple ways to show that Y is a (transitive) subclass of X –If all classes are transitive subclasses of e.g. Object, multiple inheritance always leads to diamonds A BCD E X VW Z Y

Multiple Inheritance: Semantic Issues What if multiple superclasses define the same message m or field f ? –Classic example: Artists, Cowboys, ArtistCowboys All have a draw method The draw methods access a (the?) pocket instance variable

Multiple Inheritance: Methods If V and Z both define method m, which one does Y inherit? What does super mean? –Can use directed resends: Z::m What if X defines m that Z overrides but V does not? –Can do the same thing, but often we want Z to “win” (e.g., ColorPt3D wants Pt3D ’s overrides) X VW Z Y

Multiple Inheritance: Methods Some options for method m : –Reject subclass as ambiguous – but this is too restrictive (esp. w/diamonds) –“Left-most superclass wins” – too restrictive (want per-method flexibility) + silent weirdness –Require subclass to override m (can use explicitly qualified calls to inherited methods) X VW Z Y

Multiple Inheritance: Fields Options for field f : One copy of f or multiple copies? –Multiple copies: what you want if Artist::draw and Cowboy::draw use inherited fields differently (e.g., both use a pocket variable) –Single copy: what you want for Color3dPoint x and y coordinates C++ provides both kinds of inheritance –Either two copies always, or one copy if field declared in same (parent) class X VW Z Y

Java-Style Interfaces In Java we can define interfaces and classes can implement them –Interface describes methods and types –Interface is a type – program can create variables, parameters, etc. with that type –If class C implements interface I, then instances of C have type I but must define everything in I (directly or via inheritance)

Interfaces are all about Types A Java class can have implement any number of interfaces (and also has one superclass – Object if nothing else declared) Interfaces provide no methods or fields – no duplication problems –If I1 and I2 both include some method m, implementing class must provide it somehow But this doesn’t allow what we want for Color3DPoints or ArtistCowboys –No code inheritance/reuse possible

Java Interfaces and Ruby Concept is totally irrelevant for Ruby –We can already send any message to any object (dynamic typing) –We need to get it right (can always ask an object what messages it responds to) –We don’t type-check implementers

Why no interfaces in C++? C++ allows methods and classes to be abstract –Specified in class declaration but with no implementation (same as Java) –Called pure virtual methods in C++ Abstract classes can be extended but not instantiated So a class can extend multiple abstract classes –Same as implementing interfaces But if that’s all you need, you don’t need multiple inheritance –Multiple inheritance is not just typing

Mixins A mixin is a (just) collection of methods –Less than a class: no fields, constructors, instances, etc. –More than an interface: methods have implementations Languages with mixins typically allow one superclass and any number of mixins (e.g., Ruby)

Mixin Semantics Including a mixin makes its methods part of the class –Mixins extend or override in the order they are included in the class definition –More powerful than helper methods because mixin methods can access methods and instance variables not defined in the mixin using self Not quite as powerful as multiple inheritance, but… Clear semantics, great for certain idioms ( Enumerate and Comparable using each, )

Next time Implementing inheritance, dynamic dispatch Then on Friday: wrapup, review, the end. 17