Multiple Inheritance CMPS 2143. Inheritance Heart of concept of inheritance is the is-a relationship But in the real world, objects classified in multiple,

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CS 157A: Intro to Database Sys Summary of Chapter Section 4.1 of the textbook Data Modeling and Database Design Copyright 2007 Thomson Course Technology.
Inheritance Inheritance Reserved word protected Reserved word super
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Chapter 1 Object-Oriented System Development
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Multiple Inheritance Fall 2005 OOPD John Anthony.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Chapter 7: Inheritance Abstract Data Types Object Hierarchy Inheritance of Methods Inheritance: implicit passing of information between program components.
Chapter 10 Classes Continued
Object-Oriented Design 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Abstract Classes and Interfaces
Inheritance and Polymorphism CS351 – Programming Paradigms.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of CS, Nipissing University.
CSCI-383 Object-Oriented Programming & Design Lecture 19.
SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 1 L4: Multiple Inheritance Introduction Problems of Single Inheritance and solutions Problems of Multiple.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Chapter 12 Support for Object oriented Programming.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 10 Inheritance and Polymorphism
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 12 Support for Object-Oriented Programming.
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Coming up: Inheritance
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
CSCI 383 Object-Oriented Programming & Design Lecture 19 Martin van Bommel.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
CS 2304: Multiple Inheritance
Inheritance and Polymorphism
Inheritance What is inheritance?
Types of Programming Languages
Object orientation concepts
Virtual Functions Department of CSE, BUET Chapter 10.
Overview of C++ Overloading
Support for Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
Programming in C# CHAPTER 5 & 6
Inheritance and Polymorphism
Presentation transcript:

Multiple Inheritance CMPS 2143

Inheritance Heart of concept of inheritance is the is-a relationship But in the real world, objects classified in multiple, mutually non-overlapping ways. ▫ spork is a fork and a spork is a spoon. ▫ We could have a piece of furniture that is a table and a chair (chable?) 2

Multiple Inheritance Combination of non-overlapping is-a relationships Or maybe a variation of it as as-a relationships ▫ A spork can be used as-a spoon and a spork can be used as-a fork But there are problems (sigh, aren’t there always?) 3

Example: Smalltalk’s class Magnitude Class Magnitude defines a protocol for objects that have measure – objects can be compared with one another Number – supports arithmetic operations 4

Complex Number A Complex Number is-a number and we can perform arithmetic operations on complex numbers Want to inherit from Number, want to do mixed-mode arithmetic, eg ( i) Problem: comparison of two complex numbers ▫ Complex numbers are not measurable 5

Inheritance Hiearchy Need an inheritance Hierarchy where ▫ Char is a subclass of Magnitude, but not Number ▫ Integer should be a subclass of both ▫ Complex should be a subclass of Number, but not of Magnitude 6

4 solutions 1.Make Complex a subclass of number, but redefine methods that compare to produce error messages 2.Flatten Inheritance Tree: Avoid inheritance altogether and redefine EVERY method in each of the classes Char, Integer, Complex, etc. 3.Use part of the inheritance hierarchy and simulate the rest ▫ Just redefine all comparison operators 4.Make the two classes Magnitude and Number independent of each other and use multiple inheritance 7

Solution 4: Multiple Inheritance 8

Problems (sigh) Inclusion of multiple inheritance causes 2 categories of problems 1.Name ambiguity (what if two classes inherited from each of a draw method?) ▫ Problem is with child, so child must solve  Use fully qualified names One::draw(); vs Two::draw();  Rename/redefine/overload – BUT signatures will have to be different virtual int * draw () {One::draw();} virtual void draw(Graphics g) {Two::draw(g);} 9

Problems cont. 2.Impact on substitutability ▫ Redefinition of the method name solves the problem locally ▫ BUT NOT if we try to maintain a list of ONE of the parents ▫ Example: GraphicalObject * g = new GraphicalCardDeck(); g->draw(); //will call draw method for CardDeck if //you redefined draw in GraphicalCardDeck //to call CardDeck::draw(); 10

Solution to Problem 2 Introduce new helper classes that inherit from the parents ▫ Redefine the draw operation in each to use a method of a different name class CardDeckParent : public CardDeck { public: virtual void draw (cardDeckDraw();} virtual void cardDeckDraw() {CardDeck::draw();} }; 11

Then redefine the two new methods in the child class class GraphicalCardDeck : CardDeckParent, GraphicalObjectParent { public: virtual void cardDeckDraw() {..} virtual void goDraw() {…} }; 12

GraphicalCardDeck *gcd = new GraphicalCardDeck(); CardDeck *cd = gcd; GraphicsObject *go = gcd; cd->draw(); //executes cardDeckDraw go->draw(); //executes goDraw gcd->draw(); //compiler error, ambiguous still 13

Multiple Inheritance of Interfaces C++ and Smalltalk allow multiple inheritance of classes Java and C# do not ▫ Fake it with multiple inheritance of interfaces ▫ REUSE concept, not code! 14

Study questions Pg. 273: 1, 2, 4, 6 15