25

Inheritance

Embed Size (px)

Citation preview

Page 1: Inheritance
Page 2: Inheritance

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

Page 3: Inheritance

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

Page 4: Inheritance

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

Page 5: Inheritance

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

Page 6: Inheritance

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

Page 7: Inheritance

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

Page 8: Inheritance

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

Page 9: Inheritance

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

Page 10: Inheritance

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

Page 11: Inheritance

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

Page 12: Inheritance

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

Page 13: Inheritance

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

Page 14: Inheritance

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

Page 15: Inheritance

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

Page 16: Inheritance

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

Page 17: Inheritance

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

Page 18: Inheritance

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

Page 19: Inheritance

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

Page 20: Inheritance

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

Page 21: Inheritance

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

Page 22: Inheritance

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

Page 23: Inheritance

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

Page 24: Inheritance

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

Page 25: Inheritance

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