A N E XTENSION OF P ROTÉGÉ FOR AN AUTOMA TIC F UZZY - O NTOLOGY BUILDING U...
Multiple Inheritance
1.
2. Multiple Inheritance
Traditional (C++-like) implementation of multiple inheritance has several
issues:
the diamond problem,
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 2 / 11
3. Multiple Inheritance
Traditional (C++-like) implementation of multiple inheritance has several
issues:
the diamond problem,
name clashes,
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 2 / 11
4. Multiple Inheritance
Traditional (C++-like) implementation of multiple inheritance has several
issues:
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
5. Multiple Inheritance
Traditional (C++-like) implementation of multiple inheritance has several
issues:
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
6. The Diamond Problem
class 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
7. Name Clashes
class 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
8. Name Clashes
class 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
9. Name Clashes
class 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
10. Glue Code
class 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 to
name clashes.
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 5 / 11
11. Initialization Order
In which order should we dispatch parent constructors when there are
circular dependencies?
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 5: Multiple Inheritance October 26, 2010 6 / 11
12. The Need for Multiple Inheritance
Do 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
13. The Need for Multiple Inheritance
Do 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
14. The Need for Multiple Inheritance
Do 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
15. Traits
A 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
16. Traits
A 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
17. Traits
A 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
18. Eiffel’s Multiple Inheritance
Eiffel 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
19. Eiffel’s Multiple Inheritance
Eiffel 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
20. 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
21. 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
22. 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
23. See
St´phane Ducasse, Oscar Nierstrasz, Nathanael Sch¨rli, Roel Wuyts, and
e a
Andrew P. Black. Traits: A mechanism for fine-grained reuse. ACM
Transanctions on Programming Languages and Systems 28, 2 (March
2006), 331–388. http://doi.acm.org/10.1145/1119479.1119483
Donna Malayeri and Jonathan Aldrich. CZ: multiple inheritance
without diamonds. In Proceeding of the 24th ACM SIGPLAN conference
on 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