CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 21 Late Binding; OOP as a Racket Pattern Dan Grossman Fall 2011.
Advertisements

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.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Winter 2014.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
Programming LanguagesSection 81 Programming Languages Section 8. OOP v.s. FP, Subtyping Xiaojuan Cai Spring 2015.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Winter 2013.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
CSE341: Programming Languages Lecture 22 OOP vs. Functional Decomposition; Adding Operators & Variants; Double-Dispatch Dan Grossman Spring 2013.
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
CSE 341 Programming Languages More Ruby Zach Tatlock Spring 2014.
CSE 341 Section 10 Subtyping, Review, and The Future.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 22 OOP vs
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.
CSE341: Programming Languages Lecture 22 OOP vs
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.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
Java Programming Language
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 22 OOP vs
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.
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.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Zach Tatlock Winter 2018.
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 22 OOP vs
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 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping 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:

CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013

What next? Have used classes for OOP's essence: inheritance, overriding, dynamic dispatch Now, what if we want to have more than just 1 superclass Multiple inheritance: allow > 1 superclasses –Useful but has some problems (see C++) Ruby-style mixins: 1 superclass; > 1 method providers –Often a fine substitute for multiple inheritance and has fewer problems (see also Scala traits) Java/C#-style interfaces: allow > 1 types –Mostly irrelevant in a dynamically typed language, but fewer problems Spring 20132CSE341: Programming Languages

Multiple Inheritance If inheritance and overriding are so useful, why limit ourselves to one superclass? –Because the semantics is often awkward (this topic) –Because it makes static type-checking harder (not discussed) –Because it makes efficient implementation harder (not discussed) Is it useful? Sure! –Example: Make a ColorPt3D by inheriting from Pt3D and ColorPt (or maybe just from Color ) –Example: Make a StudentAthlete by inheriting from Student and Athlete –With single inheritance, end up copying code or using non-OOP- style helper methods Spring 20133CSE341: Programming Languages

Trees, dags, and diamonds Note: The phrases subclass, superclass can be ambiguous –There are immediate subclasses, superclasses –And there are transitive subclasses, superclasses Single inheritance: the class hierarchy is a tree –Nodes are classes –Parent is immediate superclass –Any number of children allowed Multiple inheritance: the class hierarchy no longer a tree –Cycles still disallowed (a directed-acyclic graph) –If multiple paths show that X is a (transitive) superclass of Y, then we have diamonds Spring 20134CSE341: Programming Languages A BCD E X Y VW Z

What could go wrong? If V and Z both define a method m, what does Y inherit? What does super mean? –Directed resends useful (e.g., Z::super ) What if X defines a method m that Z but not V overrides? –Can handle like previous case, but sometimes undesirable (e.g., ColorPt3D wants Pt3D 's overrides to “win”) If X defines fields, should Y have one copy of them ( f ) or two ( V::f and Z::f )? –Turns out each behavior can be desirable (next slides) –So C++ has (at least) two forms of inheritance Spring 20135CSE341: Programming Languages X Y V W Z

3DColorPoints If Ruby had multiple inheritance, we would want ColorPt3D to inherit methods that share and Spring 20136CSE341: Programming Languages class Pt attr_accessor :x, :y … end class ColorPt < Pt attr_accessor :color … end class Pt3D < Pt attr_accessor :z … # override some methods end class ColorPt3D < Pt3D, ColorPt # not Ruby! end

ArtistCowboys This code has Person define a pocket for subclasses to use, but an ArtistCowboy wants two pockets, one for each draw method Spring 20137CSE341: Programming Languages class Person attr_accessor :pocket … end class Artist < Person # pocket for brush objects def draw # access pocket … end class Cowboy < Person # pocket for gun objects def draw # access pocket … end class ArtistCowboy < Artist, Cowboy # not Ruby! end

Mixins A mixin is (just) a collection of methods –Less than a class: no instances of it Languages with mixins (e.g., Ruby modules) typically let a class have one superclass but include number of mixins Semantics: Including a mixin makes its methods part of the class –Extending or overriding in the order mixins are included in the class definition –More powerful than helper methods because mixin methods can access methods (and instance variables) on self not defined in the mixin Spring 20138CSE341: Programming Languages

Example Spring 20139CSE341: Programming Languages module Doubler def double self + self # assume included in classes w/ + end class String include Doubler end class AnotherPt attr_accessor :x, :y include Doubler def + other ans = AnotherPt.new ans.x = self.x + other.x ans.y = self.y + other.y ans end

Lookup rules Mixins change our lookup rules slightly: When looking for receiver obj 's method m, look in obj 's class, then mixins that class includes (later includes shadow), then obj 's superclass, then the superclass' mixins, etc. As for instance variables, the mixin methods are included in the same object –So usually bad style for mixin methods to use instance variables since a name clash would be like our CowboyArtist pocket problem (but sometimes unavoidable?) Spring CSE341: Programming Languages

The two big ones The two most popular/useful mixins in Ruby: Comparable: Defines, ==, !=, >=, Enumerable: Defines many iterators (e.g., map, find ) in terms of each Great examples of using mixins: –Classes including them get a bunch of methods for just a little work –Classes do not “spend” their “one superclass” for this –Do not need the complexity of multiple inheritance See the code for some examples Spring CSE341: Programming Languages

Replacement for multiple inheritance? A mixin works pretty well for ColorPt3D : –Color a reasonable mixin except for using an instance variable A mixin works awkwardly-at-best for ArtistCowboy : –Natural for Artist and Cowboy to be Person subclasses –Could move methods of one to a mixin, but it is odd style and still does not get you two pockets Spring CSE341: Programming Languages module Color attr_accessor :color end module ArtistM … class Artist < Person include ArtistM class ArtistCowboy < Cowboy include ArtistM

Statically-Typed OOP Now contrast multiple inheritance and mixins with Java/C#-style interfaces Important distinction, but interfaces are about static typing, which Ruby does not have So will use Java code after quick introduction to static typing for class-based OOP… –Sound typing for OOP prevents “method missing” errors Spring CSE341: Programming Languages

Classes as Types In Java/C#/etc. each class is also a type Methods have types for arguments and result If C is a (transitive) subclass of D, then C is a subtype of D –Type-checking allows subtype anywhere supertype allowed –So can pass instance of C to a method expecting instance of D Spring CSE341: Programming Languages class A { Object m1(Example e, String s) {…} Integer m2(A foo, Boolean b, Integer i) {…} }

Interfaces are Types An interface is not a class; it is only a type –Does not contain method definitions, only their signatures (types) Unlike mixins –Cannot use new on an interface Like mixins Spring CSE341: Programming Languages interface Example { void m1(int x, int y); Object m2(Example x, String y); }

Implementing Interfaces A class can explicitly implement any number of interfaces –For class to type-check, it must implement every method in the interface with the right type More on allowing subtypes later! –Multiple interfaces no problem; just implement everything If class type-checks, it is a subtype of the interface Spring CSE341: Programming Languages class A implements Example { public void m1(int x, int y) {…} public Object m2(Example e, String s) {…} } class B implements Example { public void m1(int pizza, int beer) {…} public Object m2(Example e, String s) {…} }

Multiple interfaces Interfaces provide no methods or fields –So no questions of method/field duplication when implementing multiple interfaces, unlike multiple inheritance What interfaces are for: –“Caller can give any instance of any class implementing I ” So callee can call methods in I regardless of class –So much more flexible type system Interfaces have little use in a dynamically typed language –Dynamic typing already much more flexible, with trade-offs we studied Spring CSE341: Programming Languages

Connections Let’s now answer these questions: What does a statically typed OOP language need to support “required overriding”? How is this similar to higher-order functions? Why does a language with multiple inheritance (e.g., C++) not need Java/C#-style interfaces? [Explaining Java’s abstract methods / C++’s pure virtual methods] Spring CSE341: Programming Languages

Required overriding Often a class expects all subclasses to override some method(s) –The purpose of the superclass is to abstract common functionality, but some non-common parts have no default A Ruby approach: –Do not define must-override methods in superclass –Subclasses can add it –Creating instance of superclass can cause method-missing errors Spring CSE341: Programming Languages # do not use A.new # all subclasses should define m2 class A def m1 v … self.m2 e … end

Static typing In Java/C#/C++, prior approach fails type-checking –No method m2 defined in superclass –One solution: provide error-causing implementation –Better: Use static checking to prevent this error… Spring CSE341: Programming Languages class A def m1 v … self.m2 e … end def m2 v raise "must be overridden" end

Abstract methods Java/C#/C++ let superclass give signature (type) of method subclasses should provide –Called abstract methods or pure virtual methods –Cannot creates instances of classes with such methods Catches error at compile-time Indicates intent to code-reader Does not make language more powerful Spring CSE341: Programming Languages abstract class A { T1 m1(T2 x) { … m2(e); … } abstract T3 m2(T4 x); } class B extends A { T3 m2(T4 x) { … } }

Passing code to other code Abstract methods and dynamic dispatch: An OOP way to have subclass “pass code” to other code in superclass Higher-order functions: An FP way to have caller “pass code” to callee Spring CSE341: Programming Languages abstract class A { T1 m1(T2 x) { … m2(e); … } abstract T3 m2(T4 x); } class B extends A { T3 m2(T4 x) { … } } fun f (g,x) = … g e … fun h x = … f((fn y => …),…)

No interfaces in C++ If you have multiple inheritance and abstract methods, you do not also need interfaces Replace each interface with a class with all abstract methods Replace each “implements interface” with another superclass So: Expect to see interfaces only in statically typed OOP without multiple inheritance –Not Ruby –Not C++ Spring CSE341: Programming Languages