CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.

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

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Multiple Inheritance CMPS Inheritance Heart of concept of inheritance is the is-a relationship But in the real world, objects classified in multiple,
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
CS 211 Inheritance AAA.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 10: Introduction to Inheritance
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Multiple Inheritance Fall 2005 OOPD John Anthony.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Ch 13. Features Found Only in Java Timothy Budd Oregon State University.
Inheritance and Polymorphism CS351 – Programming Paradigms.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
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.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
CS 403 – Programming Languages Class 25 November 28, 2000.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Basic OOP Concepts and Terms. In this class, we will cover: Objects and examples of different object types Classes and how they relate to objects Object.
Chapter 12 Support for Object oriented Programming.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
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.
Object-Oriented Programming “The Rest of the Story”, CS 4450 – Chapter 16.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
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.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Interfaces Professor Evan Korth.
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Types of Programming Languages
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Workshop for Programming And Systems Management Teachers
Inheritance and Polymorphism
Presentation transcript:

CSSE501 Object-Oriented Development

Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a language allows a child class to have multiple parents. Name ambiguity Impact on substitution The Problem of Common Ancestors

Inheritance as Categorization  In one sense, the process of inheritance is a form of categorization.  A TextWindow is a type of Window, so class TextWindow inherits from class Window. But in the real world, most objects can be categorized in a variety of ways.  A person could be North American Male Professor Parent  None of these are proper subsets of the other, and we cannot make a single rooted inheritance hierarchy out of them.

Inheritance as Combination  Instead, real world objects are combinations of features from different classification schemes, each category giving some new insight into the whole: A person is North American, and The person is Male, and The person is a Professor, and The person is a Parent.  Note that we have not lost the is-a relationship; it still applies in each case.

CS Example - Complex Numbers  Two abstract classifications Magnitude - things that can be compared to each other Number - things that can perform arithmetic  Three specific classes Integer - comparable and arithmetic Char - comparable but not arithmetic Complex - arithmetic but not comparable

Possible Solutions  Make Number subclass of Magnitude, but redefine comparison operators in class complex to give error message if used. (subclassing for limitation)  Don't use inheritance at all - redefine all operators in all classes. (flattening the inheritance tree).  Use part inheritance, but simulate others - use Number, but have each number implement the comparison operations.  Make Number and Magnitude independent, and have Integer inherit from both. (multiple inheritance).

Inheritance as a form of Combination

Problem with MI - Name Ambiguity What happens when same name is used in both parent classes. A CardDeck knows how to draw a Card. A GraphicalItem knows how to draw an image on a screen. A GraphicalCardDeck should be able to draw. which?

One Solution: Redefinition  One solution is to redefine one or the other operation in the child class. class GraphicalCardDeck : public CardDeck, public GraphicalObject { public: virtual void draw () { CardDeck::draw(); } virtual void paint () { GraphicalObject::draw(); } } GraphicalCardDeck gcd; gcd.draw(); // selects CardDeck draw gcd.paint(); // selects GraphicalObject draw

Problem with Redefinition Solution  The redefinition solution is not without cost, however.  Now what happens when we run up against the principle of substitution? GraphicalObject * g = new GraphicalCardDeck(); g->draw(); // opps, doing wrong method!  This problem can be mitigated, but the solution is complex and not perfect.

Other Approaches to Name Ambiguity  Other languages use different approaches to solving the problem of ambiguous names Eiffel uses the ability to rename features from the parent class. A polymorphic variable accessing through the parents name will access the renamed feature in the child. CLOS and Python resolve ambiguous names by the order in which the parent classes are listed. The first occurrence of the name found in a systematic search is the one selected.

Multiple Inheritance of Interfaces  Multiple inheritance of interfaces does not present the same problem of name ambiguity as does multiple inheritance of classes.  Either the ambiguous methods in the parent classes have different type signatures, in which case there is no problem, or  The ambiguous methods in the parent classes have the same signature. Still no problem, since what is inherited is only a specification, not an implementation.  This is why Java permits multiple inheritance of interfaces, not of classes. Nevertheless, C# does not permit the same method name to be inherited from two parent interfaces.

Inheritance from Common Ancestors  Another problem with MI occurs when parent classes have a common root ancestor. Does the new object have one or two instances of the ancestor?

Data Field in Common Ancestor  Imagine that the common ancestor declares a data member. Should the child class have one copy of this data field, or two? Both answers can be justified with examples.  C++ gets around this by introducing the idea of a virtual parent class. If your parent is virtual there is one copy, and if not there is two.  Not a perfect solution, and makes the language complicated.

Inner Classes  The ability to next classes in C++ and Java provides a mechanism that is nearly equivalent to multiple inheritance, without the semantic problems. class Child extends ParentOne {... class InnerChild extends ParentTwo {... // can access both parents }  Within the inner class you can access methods from both parent classes. This idiom is used a lot in Java. Solves many problems that would otherwise be addressed using MI. Not exactly equivalent to MI, but very close.

Chapter Summary  In this chapter we have explored some of the problems that arise of the concept of multiple inheritance. Name ambiguity Impact on substitution The Problem of Common Ancestors