Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.

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.
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.
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,
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
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 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.
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.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
Inheritance in the Java programming language J. W. Rider.
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.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
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
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.
Variations on Inheritance Object-Oriented Programming Spring
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
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.
Programming in Java: lecture 7
Sections Inheritance and Abstract Classes
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
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
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
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 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Nicholas Shahan Spring 2016
Chapter 14 Abstract Classes and Interfaces
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
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
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:

Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08

News & Announcements Final Exam: Thur., Dec. 11, 2:30, here  Review Wed., Dec 10, 4:30, CSE 403  Old exams on website now Ignore details no longer part of the course, but several of the old compiler questions are really about parsing & grammars, which is fair game  In-class review & topic list this Friday Assignment 7 due Thursday night 11 pm  Printouts due in class Friday  NO LATE ASSIGNMENTS – even if you didn’t use all of your late days

Overview Object-oriented programming: essence is inheritance, overriding, dynamic-dispatch What about multiple inheritance (>1 superclass)?  When does it make sense?  What are the issues?

Models Multiple Inheritance: >1 superclass  Useful, but has issues (e.g., C++) Java-style interfaces: >1 type  Doesn’t apply to dynamically-typed languages; 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  Point to 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 already 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, )