1 Multiple Inheritance Fall 2005 OOPD John Anthony.

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

Multiple Inheritance CMPS Inheritance Heart of concept of inheritance is the is-a relationship But in the real world, objects classified in multiple,
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 8: More on inheritance.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Introduction to Inheritance Fall 2005 OOPD John Anthony.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance and Polymorphism CS351 – Programming Paradigms.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
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.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of CS, Nipissing University.
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015.
CSCI-383 Object-Oriented Programming & Design Lecture 19.
Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 1 L4: Multiple Inheritance Introduction Problems of Single Inheritance and solutions Problems of Multiple.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Coming up: Inheritance
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
CSCI 383 Object-Oriented Programming & Design Lecture 19 Martin van Bommel.
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.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton August 19, 2005.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Interfaces.
Inheritance and Polymorphism
Software Engineering Fall 2005
Polymorphism &Virtual Functions
Object-Oriented Programming
Polymorphism & Virtual Functions
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences
Object Oriented Programming
Designing for Inheritance
Virtual Functions Department of CSE, BUET Chapter 10.
Java Inheritance.
Chapter 10: Method Overriding and method Overloading
Method Overriding and method Overloading
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

1 Multiple Inheritance Fall 2005 OOPD John Anthony

2John J. Anthony Object Oriented Programming & Design The Problem Male North American Parent Professor Author

3John J. Anthony Object Oriented Programming & Design The (Conceptual) Solution Male North AmericanParentProfessor Author By breaking down the hierarchy, each abstract type can be combined to create valid “is-a” Relationships.

4John J. Anthony Object Oriented Programming & Design Multiple Inheritance MaleNorth AmericanParentProfessorAuthor An Author “is-a” Male and “is-a” North American and…. An Author can be viewed “as-a” Male and “as-a” North American and….

5John J. Anthony Object Oriented Programming & Design Consider… ProfessorStudent getDepartment (…) TeacherAssistant getDepartment (…) Name Ambiguity Which getDepartment(…) is Student going to inherit?

6John J. Anthony Object Oriented Programming & Design Resolving Name Ambiguity TeacherAssistant assistant; assistant->Professor::getDepartment();assistant->Student::getDepartment(); Force the client of the child class to resolve the ambiguity…

7John J. Anthony Object Oriented Programming & Design Resolving Name Ambiguity If the signatures are different (and overloading supported) then child class can simply include both methods. ProfessorStudent getDepartment () getDepartment (int majorCode) TeachingAssistant getDepartment () getDepartment (int majorCode)

8John J. Anthony Object Oriented Programming & Design Resolving Name Ambiguity Class TeachingAssistant : public Professor, public Student { public: virtual Department * getTeachingDepartment() { return Professor::getDepartment(); } virtual Department * getDepartment() { return Student::getDepartment(); }} If signatures are similar or if overloading not allowed, then programmer may override one method and rename another. What about polymorphism? Professor * p = new TeachAssistant(); p->getDepartment(); //returns the Department the TA is studying in rather than teaching in.

9John J. Anthony Object Oriented Programming & Design Resolving Name Ambiguity A C++ idiom to addressing name ambiguity…. Professor getDepartment () Student getDepartment () ProfessorParent getDepartment () StudentParent getDepartment () TeacherAssistant getTeachingDepartment() getMajorDepartment()

10John J. Anthony Object Oriented Programming & Design Idiom Con’t. Class ProfessorParent : public Professor { public: virtual Department * getDepartment() { return getTeachingDepartment(); } virtual Department * getTeachingDepartment() { return Professor ::getDepartment(); }} Professor getDepartment () ProfessorParent getDepartment () getTeachingDepartment()

11John J. Anthony Object Oriented Programming & Design TeachingAssistant * ta = new TeachingAssistant(); Professor *prof = ta; Student *student = ta; prof->getDepartment(); //OK, will execute getTeachingDepartment Student->getDepartment(); //OK, will execute getMajorDepartment ta->getDepartment(); //compiler error, ambiguous invocation Resolving Name Ambiguity

12John J. Anthony Object Oriented Programming & Design Redefinition in Eiffel cClass TeachingAssistant inheritProfessorrename getDepartment as getTeachingDepartment endStudentrename getDepartment as getMajorDepartment end

13John J. Anthony Object Oriented Programming & Design Common Ancestors A BC D AA BC D

14John J. Anthony Object Oriented Programming & Design Common Ancestors ProfessorStudent TeacherAssistant Person String *title; …the diamond of death Should there be two titles or one?