Upload
michal-pise
View
731
Download
0
Tags:
Embed Size (px)
Citation preview
What Is Subclassing?
Subclassing is a process in which a new class is derived from an old one. Itis a distinct concept from
(subtype) polymorphism,
generalization/specialization and
composition.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 2 / 12
Subclassing 6= Subtyping
Subtypes are not required to share any code with their supertypes.
On the other hand, subtypes are required to stick to the contract oftheir supertype (the Liskov substition principle).
Subtyping only concerns behavior, subclassing only concerns code.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 3 / 12
Subclassing 6= Subtyping
Subtypes are not required to share any code with their supertypes.
On the other hand, subtypes are required to stick to the contract oftheir supertype (the Liskov substition principle).
Subtyping only concerns behavior, subclassing only concerns code.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 3 / 12
Subclassing 6= Subtyping
Subtypes are not required to share any code with their supertypes.
On the other hand, subtypes are required to stick to the contract oftheir supertype (the Liskov substition principle).
Subtyping only concerns behavior, subclassing only concerns code.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 3 / 12
The Fallacy of Current Industrial Programming Languages
In Java (and many other languages)
subclassing implies subtyping, in other words,
every subclass has to stick to the contract of its parent class.
That severly limits possible code reuse!
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 4 / 12
Example
void DFS(Node n) {addToStack( n);
while ( stackNotEmpty()) {n = removeFromStack();
processNode( n);
for ( Node c: n.children())
addToStack( c);
}}
Obviously, addToStackinserts a node at thebeginning of a list andremoveFromStack removesa node from the beginningof that list.
Is it correct to overrideaddToStack to insert anode at the end of the list?
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 5 / 12
Example
void DFS(Node n) {addToStack( n);
while ( stackNotEmpty()) {n = removeFromStack();
processNode( n);
for ( Node c: n.children())
addToStack( c);
}}
Obviously, addToStackinserts a node at thebeginning of a list andremoveFromStack removesa node from the beginningof that list.
Is it correct to overrideaddToStack to insert anode at the end of the list?
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 5 / 12
Example
void DFS(Node n) {addToStack( n);
while ( stackNotEmpty()) {n = removeFromStack();
processNode( n);
for ( Node c: n.children())
addToStack( c);
}}
Obviously, addToStackinserts a node at thebeginning of a list andremoveFromStack removesa node from the beginningof that list.
Is it correct to overrideaddToStack to insert anode at the end of the list?
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 5 / 12
Example (II)
Not in Java—the new subtype would break the contract of thesupertype.
However, it is perfectly legitimate in languages where subclassingdoes not imply subtyping—the new class simply isn’t type-compatiblewith the old one.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 6 / 12
Example (II)
Not in Java—the new subtype would break the contract of thesupertype.
However, it is perfectly legitimate in languages where subclassingdoes not imply subtyping—the new class simply isn’t type-compatiblewith the old one.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 6 / 12
Subclassing 6= Specialization
Subclassing does not imply specialization—subclass does not have tobe a specialized version of superclass.
Specialization does not imply subclassing—a specialized version ofobject does not have to share code with the base case.
In fact, subtyping and specialization are distinct notions as well.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 7 / 12
Subclassing 6= Specialization
Subclassing does not imply specialization—subclass does not have tobe a specialized version of superclass.
Specialization does not imply subclassing—a specialized version ofobject does not have to share code with the base case.
In fact, subtyping and specialization are distinct notions as well.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 7 / 12
Subclassing 6= Specialization
Subclassing does not imply specialization—subclass does not have tobe a specialized version of superclass.
Specialization does not imply subclassing—a specialized version ofobject does not have to share code with the base case.
In fact, subtyping and specialization are distinct notions as well.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 7 / 12
Subclassing 6= Composition
Subclassing and composition both facilitate code reuse.
Is there anything that can be achieved by subclassing and notachieved by composition or vice versa?
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 8 / 12
Poor Man’s Inheritance
class Foo {void bar() {...}int baz() {...}
}
class Qux {Foo foo = new Foo();
void bar() {foo.bar();
}
int baz() {return foo.baz();
}}
Emulation of inheritance using composition is far from perfect—methodoverriding can be emulated only in the simplest cases.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 9 / 12
Poor Man’s Inheritance
class Foo {void bar() {...}int baz() {...}
}
class Qux {Foo foo = new Foo();
void bar() {foo.bar();
}
int baz() {return foo.baz();
}}
Emulation of inheritance using composition is far from perfect—methodoverriding can be emulated only in the simplest cases.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 9 / 12
The Essence of Inheritance
So what exactly is inheritance?
Inheritance is a mechanism facilitating code reuse.
It can be thought about as the C-style include with overriding.
It enables reuse even in ways the original code creator never imagined.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 10 / 12
The Essence of Inheritance
So what exactly is inheritance?
Inheritance is a mechanism facilitating code reuse.
It can be thought about as the C-style include with overriding.
It enables reuse even in ways the original code creator never imagined.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 10 / 12
The Essence of Inheritance
So what exactly is inheritance?
Inheritance is a mechanism facilitating code reuse.
It can be thought about as the C-style include with overriding.
It enables reuse even in ways the original code creator never imagined.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 10 / 12
The Essence of Inheritance
So what exactly is inheritance?
Inheritance is a mechanism facilitating code reuse.
It can be thought about as the C-style include with overriding.
It enables reuse even in ways the original code creator never imagined.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 10 / 12
Implementation of Inheritance
C++: virtual methods table.
In Smalltalk: method dictionary.
In systems with runtime: inline cache.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 11 / 12
Implementation of Inheritance
C++: virtual methods table.
In Smalltalk: method dictionary.
In systems with runtime: inline cache.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 11 / 12
Implementation of Inheritance
C++: virtual methods table.
In Smalltalk: method dictionary.
In systems with runtime: inline cache.
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 11 / 12
See
Taivalsaari, A. On the notion of inheritance. ACM Computing Surveys28, 3 (Sep. 1996), 438–479.http://doi.acm.org/10.1145/243439.243441
Michal Pıse (CTU in Prague) Object Programming Lect. 4: Inheritance October 19, 2010 12 / 12