CSCI-383 Object-Oriented Programming & Design Lecture 19.

Slides:



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

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
Introduction To System Analysis and Design
Introduction to Computers and Java Recitation - 01/11/2008 CS 180 Department of Computer Science, Purdue University.
Advanced Object-Oriented Programming Features
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Inheritance and Composition.
Computer Science I Inheritance Professor Evan Korth New York University.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Introduction To System Analysis and design
Chapter 12: Adding Functionality to Your Classes.
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.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
CSCI-383 Object-Oriented Programming & Design Lecture 9.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of CS, Nipissing University.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12 Support for Object oriented Programming.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
Inheritance and Access Control CS 162 (Summer 2009)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Object-Oriented Programming Chapter Chapter
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Chapter 12 Object-oriented design for more than one class.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
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)
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
CSCI 383 Object-Oriented Programming & Design Lecture 19 Martin van Bommel.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOPS CONCEPT.  OOPS  Benefits of OOPs  OOPs Principles  Class  Object Objectives.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
CS Introduction to C++ Inheritance Topic #3.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
ISBN Chapter 12 Support for Object-Oriented Programming.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Chapter 13 Abstract Classes and Interfaces
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 11: Inheritance and Composition
Java Programming Language
C++ Object Oriented 1.
Presentation transcript:

CSCI-383 Object-Oriented Programming & Design Lecture 19

Chapter 13 Multiple Inheritance

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Inheritance as Classification  In one way, the is-a relationship is a form of classification E.g., A TextFile is a type of File so class TextFile inherits from class File File TextFile

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Inheritance as Classification  But in the real world, most objects can be categorized in a variety of ways. A person can be a Male Professor Parent  None of these are proper subsets of the other, and we cannot make a single rooted inheritance hierarchy out of them

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Inheritance as Combination  Instead, real world objects are combinations of many nonoverlapping categories, each category contributing something to the result  Note that we have not lost the is-a relationship; it still applies in each case MaleProfessor Parent

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Multiple Inheritance  Modeling this behavior in programs seems to call for the concept of multiple inheritance An object can have two or more different parent classes and inherit both data and behavior from each MaleProfessorParent

Multiple Inheritance  It wouldn't be an exaggeration to say that multiple inheritance has stirred more controversy and heated debates than has any other C++ feature  Yet the truth is that multiple inheritance is a powerful and effective feature -- when used properly  What is a good model?

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Incomparable Complex Numbers  A portion of the Smalltalk class hierarchy Object BooleanMagnitudeCollection CharNumberPointSetKeyedCollectionTrueFalse IntegerFloatFraction things that can be compared to each other things that can perform arithmetic

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Possible Solutions  Make Number subclass of Magnitude, but redefine comparison operators in class Complex to give error message if used (e.g., subclassing for limitation)  Don't use inheritance at all -- redefine all operators in all classes (e.g., flattening the inheritance tree)  Use part inheritance, but simulate others (e.g., use Number, but have each number implement all relational operators)  Make Number and Magnitude independent, and have Integer inherit from both (i..e, multiple inheritance)

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Possible Solutions MagnitudeNumber CharIntegerComplex

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Another Example – Walking Menus  A Menu is a structure charged with displaying itself when selected by the user  A Menu maintains a collection of MenuItems  Each MenuItem knows how to respond when selected  A cascading menu is both a MenuItem and a Menu

Multiple Inheritance in C++  C++ supports multiple inheritance (i.e., a class may be derived from more than one base class)  C++ syntax: class Derived: public Base1, public Base2, public Base3, protected Base4 {... };  Example: class passengerVehicle {... }; class trainCar {... }; class passengerCar: public passengerVehicle, public trainCar {... };  Handout #6 Handout #6

The Ambiguity Problem  Circumstances: Let Derived be inherited from Base1 and Base2 All feature names inside Base1 are distinct All feature names inside Base2 are distinct All feature names inside Derived should be distinct  Ambiguity problem: the same feature name X occurs both in Base1 and in Base2 The problem does not occur in single inheritance  If the same feature name occurs both in Derived and in Base, then no ambiguity occurs. Why?

The Ambiguity Problem  In handout #6, what would happen if we attempted to print the data members of derived by accessing them individually instead of using the overloaded operator<<handout #6 cout << “Object derived contains:\n” << “Integer: “ << derived.getData() << “\nCharacter: “ << derived.getData() << “\nReal number: “ << derived.getReal() << “\n\n“;

Coincidental vs. Inherent Ambiguity  Coincidental ambiguity occurs when the duplicated names Base1:: X and Base2:: X are unrelated Ambiguity is a coincidence Same name, distinct entities  Inherent ambiguity occurs when Base1 and Base2 derive from a common Base, in which X occurs Ambiguity is inherent Same name, same entity

Ambiguity Resolution Approaches  Forbid the inheritance as given. Force the designer of Base1 and/or Base2 to resolve the ambiguity Unreasonable: Good names are rare. In many cases, multiple inheritance marries together two distinct inheritance hierarchies, sometimes supplied by different vendors Impossible: In cases of repeated inheritance  Force the designer of Derived to resolve all ambiguities In Eiffel, the compiler does not allow a class in which an ambiguity exists. The designer must rename all ambiguous features In C++, a good designer will override all ambiguous features. This is not always possible since one cannot override data members

Ambiguity Resolution in C++  Scenario An inherited function/data member is used (inside or outside the class) The compiler checks that the name is defined in exactly one (direct/indirect) base class Error message produced if name is defined in more than one class Ambiguity should be resolved before compilation can proceed  Use qualified names ( :: ) to specify exactly one derived member

Ambiguity Resolution in C++  In handout #6, if we wanted to print the data members of derived by accessing them individually instead of using the overloaded operator<<, we could use the scope resolution operator ( :: ) to resolve the ambiguityhandout #6 cout << “Object derived contains:\n” << “Integer: “ << derived.Base1::getData() << “\nCharacter: “ << derived.Base2::getData() << “\nReal number: “ << derived.getReal() << “\n\n“;  Drawbacks Client should be aware of implementation details Late detection of errors