SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 1 L4: Multiple Inheritance Introduction Problems of Single Inheritance and solutions Problems of Multiple.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Inheritance Classes and Subclasses Or Extending a Class.
Introduction to Java 2 Programming
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
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.
Chapter 1 Inheritance University Of Ha’il.
Multiple Inheritance CMPS Inheritance Heart of concept of inheritance is the is-a relationship But in the real world, objects classified in multiple,
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
OOP: Inheritance By: Lamiaa Said.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Inheritance and Polymorphism CS351 – Programming Paradigms.
1 CSE 303 Lecture 24 Inheritance in C++, continued slides created by Marty Stepp
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of CS, Nipissing University.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
CSCI-383 Object-Oriented Programming & Design Lecture 19.
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.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
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.
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
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.
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
CSCI 383 Object-Oriented Programming & Design Lecture 19 Martin van Bommel.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
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.
Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,
Comp 249 Programming Methodology
Software Design Lecture : 12.
Chapter 11: Inheritance and Composition
Lecture 10 Concepts of Programming Languages
Chapter 7 Inheritance.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 1 L4: Multiple Inheritance Introduction Problems of Single Inheritance and solutions Problems of Multiple Inheritance: Name ambiguity Substitutability Common ancestors 5 Solutions to the Problems Virtual Inheritance Inner Classes Chapter 13 of Budd

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 2 Multiple Inheritance Where a child class can inherit from more than one parent class directly Supported in C++, Eiffel Not supported in Java, C#

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 3 A Quote Multiple inheritance allows several objects to act as base objects... The characteristics of several different object classes can be combined to make up a new object. For example, say we have an object class CAR and an object class PERSON. We could use both of these to define a new object class CAR-OWNER … Sommerville, “Software Engineering”, 4th Edition

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 4 Problem of Single Inheritance We can divide the world into an infinite number of hierarchies For complex problems strict hierarchies almost always end up with trade-offs and ambiguities.

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 5 Example Object Ordered Char Number Integer Float FractionComplex?

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 6 Solutions Make Complex a subclass of Number and override Ordered methods Avoid inheritance, and redefine all methods Use a partial inheritance hierarchy Use multiple inheritance

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 7 Problems of Multiple Inheritance Name ambiguity Substitutability Common ancestors

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 8 Name Ambiguity The same name can mean different things to different ancestors GraphicalCardDeck CardDeck draw()‏ GraphicalObject draw()‏

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 9 Solution A Explicit disambiguation: GraphicalCardDeck gcd; gcd.CardDeck::draw(); gcd.GraphicalObject::draw();

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 10 Solution B With different type signatures, can use straight overloading: class GCD : public CardDeck, public GO { public: virtual Card *draw(){ return CardDeck::draw();}; virtual void draw(GraphicsContext &g){ GraphicalObject::draw(g);}; };

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 11 Solution B2 class GCD : public CardDeck, public GO { public: using CardDeck::draw; using GraphicalObject::draw; };

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 12 Solution C With the same type signatures, we're in trouble: class GCD: public CardDeck, public GO { public: virtual void draw(){ return CardDeck::draw();} virtual void paint(){GO::draw();}; }; GraphicalObject *g = new GraphicalCardDeck(); g->draw();

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 13 Solution D class CardDeckParent : public CardDeck { virtual void draw() {cardDeckdraw();}; virtual void cardDeckDraw() {CardDeck::draw();}; }; class GraphicalObjectParent: public GO { virtual void draw() {goDraw();}; virtual void goDraw() {GO::draw();}; }; class GraphicalCardDeck:public CDP,public GOP { virtual void cardDeckDraw() {CDP::cardDeckDraw();}; virtual void goDraw() {GOP::goDraw();}; };

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 14 Solution D GraphicalCardDeck *gcd = new GCD(); CardDeck *cd = dynamic_cast (gcd); GraphicalObject *go = dynamic_cast (gcd); cd->draw(); // ok go->draw(); // ok gcd->cardDeckDraw(); // ok gcd->goDraw(); // ok gcd->draw(); // error - ambiguous

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 15 Common Ancestors The “diamond of death”: A BC D AA BC D

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 16 C++ Example StreamInStream OutStream InOutStream InStream and OutStream inherit and override a method in Stream. InOutStream inherits the same method.

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 17 Virtual Inheritance class Stream {...}; class InStream: public virtual Stream {...}; class OutStream: public virtual Stream {...}; class InOutStream: public InStream, public OutStream {...}; The ambiguity occurs in InOutStream, and the solution involves the definitions of InStream and OutStream.

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 18 Inner Classes In languages which allow inner classes (Java, C#), we can simulate multiple inheritance after a fashion:

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 19 Java Inner Classes class GraphicalCardDeck extends CardDeck { public void draw() { // draw a card from a deck} private class DrawingClass extends GraphicalObject { public void draw() {// draw a card on screen} } private DrawingClass drawer = new DrawingClass; public GraphicalObject myDrawingObject(){ return drawer;}; }