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

Slides:



Advertisements
Similar presentations
Object Oriented Programming
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.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
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,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
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 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
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.
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.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 12 Support for Object oriented Programming.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
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.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
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
Coming up: Inheritance
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
CSCE 240 – Intro to Software Engineering Lecture 3.
ISBN Chapter 12 Support for Object-Oriented Programming.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Sections Inheritance and Abstract Classes
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
Inheritance and Polymorphism
CSE 341 Section 9 Nick Mooney Spring 2017
Types of Programming Languages
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Zach Tatlock Winter 2018.
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
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.
Object Oriented Programming
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
Nicholas Shahan Spring 2016
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 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 Ruby: 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 –Useful – factor different traits/behavior to 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? –Semantic and implementation complexities –Typing issues w/static typing Is it useful? Sure: –Color3DPoint extends 3DPoint, ColorPoint Naïve view: subclass has all fields and methods of all superclasses

Trees, DAGs, and Diamonds Class hierarchy forms a graph –Edges from subclasses to superclasses –Single inheritance: a tree –Multiple inheritance: a DAG Diamonds –With multiple inheritance, may be multiple ways to show that A is a (transitive) subclass of B –If all classes are transitive subclasses of e.g. Object, multiple inheritance always leads to diamonds

Multiple Inheritance: Semantic Issues What if multiple superclasses define the same message m or field f ? –Classic example: Artists, Cowboys, ArtistCowboys 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)

Multiple Inheritance: Semantic Issues 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 –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

Java-Style Interfaces In Java we can define interfaces and classes can implement them –Interface describes methods and types –Interface is a type – can have 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 In Java, we can have 1 immediate superclass and implement any number of interfaces 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)

Interfaces vs Abstract Classes Interfaces are not needed in C++. Why? C++ allows methods and classes to be abstract –Specified in class declaration but not provided in implementation (same as Java) –Called pure virtual methods in C++ 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 collection of methods –No fields, constructors, instances, etc. Typically a language with mixins allows 1 superclass and any number of mixins –We’ve seen this in Ruby Bad news: less powerful than multiple inheritance (what is in a class, what is in a mixin?) Good news: Clear semantics, great for certain idioms (Enumerate, Comparable using each, )