Multiple Inheritance

659 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
659
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Multiple Inheritance

  1. 1. Multiple InheritanceTraditional (C++-like) implementation of multiple inheritance has severalissues: the diamond problem, Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 2 / 11
  2. 2. Multiple InheritanceTraditional (C++-like) implementation of multiple inheritance has severalissues: the diamond problem, name clashes, Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 2 / 11
  3. 3. Multiple InheritanceTraditional (C++-like) implementation of multiple inheritance has severalissues: the diamond problem, name clashes, glue code and Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 2 / 11
  4. 4. Multiple InheritanceTraditional (C++-like) implementation of multiple inheritance has severalissues: the diamond problem, name clashes, glue code and initialization order. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 2 / 11
  5. 5. The Diamond Problemclass Foo {...}class Bar extends Foo {...}class Baz extends Foo {...}class Qux extends Bar, Baz {...}How many instances of Foo should be in one instance of Qux? Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 3 / 11
  6. 6. Name Clashesclass Foo { void bar() {...} }class Baz { void bar() {...} }class Qux extends Foo, Baz {...} Which of the two methods named bar gets called during execution of new Qux().bar()? Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 4 / 11
  7. 7. Name Clashesclass Foo { void bar() {...} }class Baz { void bar() {...} }class Qux extends Foo, Baz {...} Which of the two methods named bar gets called during execution of new Qux().bar()? What if we need to keep both implementations of bar? Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 4 / 11
  8. 8. Name Clashesclass Foo { void bar() {...} }class Baz { void bar() {...} }class Qux extends Foo, Baz {...} Which of the two methods named bar gets called during execution of new Qux().bar()? What if we need to keep both implementations of bar? Is it even type-safe to discard one of bar implementations? What about the Liskov substitution principle? Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 4 / 11
  9. 9. Glue Codeclass Foo { class Qux { void bar() { baz(); } void corge() {...} abstract void baz(); }}class Grault extends Foo, Qux { void baz() { corge(); }}Grault.baz exists only to connect Foo.baz and Qux.corge.In a simplified way, glue code can be thought of as an opposite problem toname clashes. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 5 / 11
  10. 10. Initialization OrderIn which order should we dispatch parent constructors when there arecircular dependencies? Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 6 / 11
  11. 11. The Need for Multiple InheritanceDo we even need multiple inheritance? Is it worth the trouble? Yes. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 7 / 11
  12. 12. The Need for Multiple InheritanceDo we even need multiple inheritance? Is it worth the trouble? Yes. Units of reuse are smaller than units of instantiation. To compose a class, many smaller reusable chunks of code need to be composed together. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 7 / 11
  13. 13. The Need for Multiple InheritanceDo we even need multiple inheritance? Is it worth the trouble? Yes. Units of reuse are smaller than units of instantiation. To compose a class, many smaller reusable chunks of code need to be composed together. The alternative is code duplication. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 7 / 11
  14. 14. TraitsA recent multiple inheritance model.When inheriting, traits allow to create method aliases (to partially resolve name clashes and name mismatches), Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 8 / 11
  15. 15. TraitsA recent multiple inheritance model.When inheriting, traits allow to create method aliases (to partially resolve name clashes and name mismatches), drop methods (to get rid of unwanted functionality or to avoid the diamond problem) and Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 8 / 11
  16. 16. TraitsA recent multiple inheritance model.When inheriting, traits allow to create method aliases (to partially resolve name clashes and name mismatches), drop methods (to get rid of unwanted functionality or to avoid the diamond problem) and break the Liskov substitution principle (they do not imply subtyping and therefore subsumption is not an issue). Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 8 / 11
  17. 17. Eiffel’s Multiple InheritanceEiffel approach: allow method renaming (name clashes can be resolved while sticking to the Liskov substitution principle) and Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 9 / 11
  18. 18. Eiffel’s Multiple InheritanceEiffel approach: allow method renaming (name clashes can be resolved while sticking to the Liskov substitution principle) and utilize static type information to keep track of method renaming. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 9 / 11
  19. 19. CZ (Cubic Zirconia) Two kinds of inheritance hierarchies: extends and requires. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 10 / 11
  20. 20. CZ (Cubic Zirconia) Two kinds of inheritance hierarchies: extends and requires. Extends hierarchy can not contain cycles. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 10 / 11
  21. 21. CZ (Cubic Zirconia) Two kinds of inheritance hierarchies: extends and requires. Extends hierarchy can not contain cycles. Initialization is based on extends hierarchy. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 10 / 11
  22. 22. SeeSt´phane Ducasse, Oscar Nierstrasz, Nathanael Sch¨rli, Roel Wuyts, and e aAndrew P. Black. Traits: A mechanism for fine-grained reuse. ACMTransanctions on Programming Languages and Systems 28, 2 (March2006), 331–388. http://doi.acm.org/10.1145/1119479.1119483Donna Malayeri and Jonathan Aldrich. CZ: multiple inheritancewithout diamonds. In Proceeding of the 24th ACM SIGPLAN conferenceon Object oriented programming systems languages and applications(OOPSLA ’09). 21–40.http://doi.acm.org/10.1145/1640089.1640092 Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 11 / 11

×