120
A Critical Look at Inheritance “You can choose your associates, “You can choose your associates, but you’re stuck with your ancestors.”

A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

A Critical Look at Inheritance

“You can choose your associates, “You can choose your associates, but you’re stuck with your

ancestors.”

Page 2: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance in OOD

• Inheritance is often held to be sacrosanct in OOD.

• Tendency for OO developers to gauge the success of their efforts by the complexity of their inheritance hierarchy.their inheritance hierarchy.

• It is interesting to note that inheritance hierarchy examples in OO texts seldom deal with software design problems.

Page 3: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance--The Reality• Inheritance is a complex issue.

– Many different types of inheritance relationships.

– Basic notions differ among OO languages

– Some controversial issues--e.g. multiple inheritance.

– Inheritance can break encapsulation.– Inheritance can break encapsulation.

– Poorly conceived inheritance relationships can frustrate system reliability, maintainability, and evolvability.

• Inheritance is neither inherently good or bad. It must be used in a disciplined manner.

Page 4: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance--A Simple Classification

• Subclassing– inheritance of implementation fragments/code

from a superclass.

• Interface Inheritance• Interface Inheritance– inheritance of contract fragments/interfaces.

Page 5: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

The Complexities of Subclassing

• Methods of a class may freely invoke each other.

• Subclasses may override inherited methods.

• Subclass methods may call methods of • Subclass methods may call methods of superclasses, including overridden superclass methods.

• This is actually a form of “callback” from subclass to superclass.

Page 6: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Issues Example

Text-text: Array of Char-used:Integer = 0-caret:Integer = 0

+max( ):Integer+length( ):Integer+write(pos:Integer, ch:Char)

+setCaret(pos:Integer)

-cacheX: Integer = 0-cacheY: Integer = 0

SimpleText

+write(pos:Integer, ch:Char)+delete(pos:Integer)+caretPos( ):Integer+setCaret(pos:Integer)posToXCoord(pos:Integer):IntegerposToYCoord(pos:Integer):IntegerposFromCoord(x:Integer,

y:Integer):Integer+type(Char)+rubout( )

+setCaret(pos:Integer)+posToXCoord(pos:Integer):Integer+posToYCoord(pos:Integer):Integer+posFromCoord(x:Integer,

y:Integer):Integer+hideCaret( )+showCaret( )

Page 7: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--Continuedabstract class Text {

.

.

.private int caret = 0;

.

.

class SimpleText extends Text {...

void setCaret(int pos) {int old = caretPos( );if (old != pos) {.

.void setCaret(int pos) {caret = pos;}

.

.

.}

if (old != pos) {hideCaret( );super.setCaret(pos);showCaret( );

}...

}

Page 8: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--ContinuedInteraction diagram resulting from call to method type of Text class:

Text SimpleText Display

typecaretPos

writesetCaretsetCaret

hideCaret

(update display)Super.setCaret

showCaret

setCaret

Page 9: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--ContinuedA new version of Text class that “breaks” the subclass SimpleText:

abstract class Text {...

void write (int pos, char ch) {int i;int i;for ( i = used; i > pos; i--)

text[i] = text[i-1];used = used + 1;if (caret >= pos)

caret = caret +1;text[pos] = ch;

}...

Page 10: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Issues--The Fragile Base Class Problem

• There is an implicit interface between a class and its ancestor classes (superclasses).– Syntactic aspect--Does a class need to be

recompiled due to purely syntactic changes recompiled due to purely syntactic changes among it superclasses?

– Semantic Aspect--How dependent is a subclass upon changes in the implementation of its superclasses?

Page 11: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Dealing With Class-Subclass Dependencies

• Specialization Interface– Interface between a class and its subclasses

– For Java and C++, the specialization interface consists of the public and protected interface of the superclass.

• Various methods have been proposed to control behavior across a specialization interface, but these are largely of theoretical interest.

Page 12: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Alternatives to Inheritance--Object Composition

• Object composition--composition of behavior based upon references among objects rather than inheritance relations.

• Based upon “part of” relationship among • Based upon “part of” relationship among objects.– Suppose object A requests help from object B

– B is “part of” A if references to B do not leave A.

Page 13: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Object Composition

B

innerouter(owning)

CA(owned)objects

(owning)object

Note: A reuses the implementation of objects B and C

Page 14: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Composition Versus Inheritance

• An instantiated object has one notion of self even though it may inherit parts of its implementation from several superclasses.

• “Self-recursive” invocations of methods always return to the overriding version in always return to the overriding version in the lowest level subclass

• Composed objects do not have a common self—i.e.outer object does not share identity with inner objects.

Page 15: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example of self-recursive calls

BaseA( )

Sub

Base SubSubSub

A( )

uses A( )

B( )

super.A( )super.A( )usesSub

A( )

SubSubA( )

super.A( )usessuper.A( )

usessuper.A

Page 16: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example of Composition

BaseA”( )B( )

uses

:Base:SubSub :Sub

AA’

A’’

SubA’( )

SubSubA( )

uses

uses

Page 17: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Composition--Additional Observations• Composition requires that object

interactions, including recursive interactions among objects, be explicitly designed-in rather than an implicit by-product of implementation inheritance.implementation inheritance.

• Composition is a relationship between instantiated objects, not a relationship between classes.

• Composition can be made as general as subclassing by use of delegation.

Page 18: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance versus CompositionAn example of multiple inheritance from

a well-known OO textAPPLE PIE

APPLE PIE

Does this make sense?

Page 19: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Apple Pie Example-Continued

A more sensible approach

APPLE PIE

APPLE

1

*PIE SHELL

1

1

Page 20: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Apple Pie example--A Compositional Perspective

Apple Pie

Pie ShellApple

Page 21: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Versus Composition--Another Example

• Inheritance is generally not appropriate for “is a role played by” relationships.

• For instance, consider roles in an airline reservation system:reservation system:– passenger

– ticket agent

– flight crew

– etc.

Page 22: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A Potential Inheritance Hierarchy

Person

CrewMember TicketAgent Passenger

Is there a problem with this approach?

Page 23: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A Potential Inheritance Hierarchy

Person

CrewMember TicketAgent Passenger

Problem with this approach: a person may play different roles. An instantiated subclass can only represent one role.

Page 24: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--An Attempt to Fix the Inheritance Hierarchy

Person

CrewMember Passenger TicketAgentCrewMember Passenger TicketAgent

CrewMemberAndTicketAgent

CrewMemberAndPassenger

TicketAgentAndPassenger

CrewMemberAndTicketAgentAndPassenger

Page 25: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A More Rational Solution using Composition

CrewMember TicketAgent Passenger

Uses Uses Uses0..1 0..1 0..1

Person

Uses Uses Uses

1 1

1

Note: Many authors refer to this as delegation.

Page 26: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

TicketAgentObject

PassengerObject

Object Encapsulation via Composition

PersonObject

PersonObject

Page 27: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

An Alternative Composition for the Roles Example

CrewMember TicketAgent Passenger

Uses Uses Uses0..1 0..1 0..1

Person

Uses Uses Uses

1 1

1

Page 28: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Object Encapsulation for Alternative Composition

PersonPerson

PassengerTicketAgent Flight

CrewTicketAgent

Page 29: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Versus Composition--Some Guidelines

• It is generally not a good idea to use inheritance for the following purposes:– To represent dynamically changing alternative roles of

a superclass

– To hide methods or attributes inherited from a – To hide methods or attributes inherited from a superclass (restriction)

– To implement a domain-specific class as a subclass of a utility class (specialization)

– Purely for purposes of obtaining functionality needed to implement the subclass (implementation inheritane)/

Page 30: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Potential Drawbacks of Composition (Delegation)

• There may be some minor performance penalty for invoking an operation across object boundaries as opposed to using an inherited method.inherited method.

• Composition can’t be used with partially abstract (uninstantiable) classes

• Composition does not, in and of itself, impose any disciplined structure on the design (but neither does a class hierarchy).

Page 31: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

A Critical Look at Inheritance

“You can choose your associates, “You can choose your associates, but you’re stuck with your

ancestors.”

Page 32: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance in OOD

• Inheritance is often held to be sacrosanct in OOD.

• Tendency for OO developers to gauge the success of their efforts by the complexity of their inheritance hierarchy.their inheritance hierarchy.

• It is interesting to note that inheritance hierarchy examples in OO texts seldom deal with software design problems.

Page 33: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance--The Reality• Inheritance is a complex issue.

– Many different types of inheritance relationships.

– Basic notions differ among OO languages

– Some controversial issues--e.g. multiple inheritance.

– Inheritance can break encapsulation.– Inheritance can break encapsulation.

– Poorly conceived inheritance relationships can frustrate system reliability, maintainability, and evolvability.

• Inheritance is neither inherently good or bad. It must be used in a disciplined manner.

Page 34: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance--A Simple Classification

• Subclassing– inheritance of implementation fragments/code

from a superclass.

• Interface Inheritance• Interface Inheritance– inheritance of contract fragments/interfaces.

Page 35: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

The Complexities of Subclassing

• Methods of a class may freely invoke each other.

• Subclasses may override inherited methods.

• Subclass methods may call methods of • Subclass methods may call methods of superclasses, including overridden superclass methods.

• This is actually a form of “callback” from subclass to superclass.

Page 36: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Issues Example

Text-text: Array of Char-used:Integer = 0-caret:Integer = 0

+max( ):Integer+length( ):Integer+write(pos:Integer, ch:Char)

+setCaret(pos:Integer)

-cacheX: Integer = 0-cacheY: Integer = 0

SimpleText

+write(pos:Integer, ch:Char)+delete(pos:Integer)+caretPos( ):Integer+setCaret(pos:Integer)posToXCoord(pos:Integer):IntegerposToYCoord(pos:Integer):IntegerposFromCoord(x:Integer,

y:Integer):Integer+type(Char)+rubout( )

+setCaret(pos:Integer)+posToXCoord(pos:Integer):Integer+posToYCoord(pos:Integer):Integer+posFromCoord(x:Integer,

y:Integer):Integer+hideCaret( )+showCaret( )

Page 37: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--Continuedabstract class Text {

.

.

.private int caret = 0;

.

.

class SimpleText extends Text {...

void setCaret(int pos) {int old = caretPos( );if (old != pos) {.

.void setCaret(int pos) {caret = pos;}

.

.

.}

if (old != pos) {hideCaret( );super.setCaret(pos);showCaret( );

}...

}

Page 38: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--ContinuedInteraction diagram resulting from call to method type of Text class:

Text SimpleText Display

typecaretPos

writesetCaretsetCaret

hideCaret

(update display)Super.setCaret

showCaret

setCaret

Page 39: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--ContinuedA new version of Text class that “breaks” the subclass SimpleText:

abstract class Text {...

void write (int pos, char ch) {int i;int i;for ( i = used; i > pos; i--)

text[i] = text[i-1];used = used + 1;if (caret >= pos)

caret = caret +1;text[pos] = ch;

}...

Page 40: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Issues--The Fragile Base Class Problem

• There is an implicit interface between a class and its ancestor classes (superclasses).– Syntactic aspect--Does a class need to be

recompiled due to purely syntactic changes recompiled due to purely syntactic changes among it superclasses?

– Semantic Aspect--How dependent is a subclass upon changes in the implementation of its superclasses?

Page 41: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Dealing With Class-Subclass Dependencies

• Specialization Interface– Interface between a class and its subclasses

– For Java and C++, the specialization interface consists of the public and protected interface of the superclass.

• Various methods have been proposed to control behavior across a specialization interface, but these are largely of theoretical interest.

Page 42: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Alternatives to Inheritance--Object Composition

• Object composition--composition of behavior based upon references among objects rather than inheritance relations.

• Based upon “part of” relationship among • Based upon “part of” relationship among objects.– Suppose object A requests help from object B

– B is “part of” A if references to B do not leave A.

Page 43: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Object Composition

B

innerouter(owning)

CA(owned)objects

(owning)object

Note: A reuses the implementation of objects B and C

Page 44: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Composition Versus Inheritance

• An instantiated object has one notion of self even though it may inherit parts of its implementation from several superclasses.

• “Self-recursive” invocations of methods always return to the overriding version in always return to the overriding version in the lowest level subclass

• Composed objects do not have a common self—i.e.outer object does not share identity with inner objects.

Page 45: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example of self-recursive calls

BaseA( )

Sub

Base SubSubSub

A( )

uses A( )

B( )

super.A( )super.A( )usesSub

A( )

SubSubA( )

super.A( )usessuper.A( )

usessuper.A

Page 46: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example of Composition

BaseA”( )B( )

uses

:Base:SubSub :Sub

AA’

A’’

SubA’( )

SubSubA( )

uses

uses

Page 47: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Composition--Additional Observations• Composition requires that object

interactions, including recursive interactions among objects, be explicitly designed-in rather than an implicit by-product of implementation inheritance.implementation inheritance.

• Composition is a relationship between instantiated objects, not a relationship between classes.

• Composition can be made as general as subclassing by use of delegation.

Page 48: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance versus CompositionAn example of multiple inheritance from

a well-known OO textAPPLE PIE

APPLE PIE

Does this make sense?

Page 49: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Apple Pie Example-Continued

A more sensible approach

APPLE PIE

APPLE

1

*PIE SHELL

1

1

Page 50: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Apple Pie example--A Compositional Perspective

Apple Pie

Pie ShellApple

Page 51: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Versus Composition--Another Example

• Inheritance is generally not appropriate for “is a role played by” relationships.

• For instance, consider roles in an airline reservation system:reservation system:– passenger

– ticket agent

– flight crew

– etc.

Page 52: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A Potential Inheritance Hierarchy

Person

CrewMember TicketAgent Passenger

Is there a problem with this approach?

Page 53: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A Potential Inheritance Hierarchy

Person

CrewMember TicketAgent Passenger

Problem with this approach: a person may play different roles. An instantiated subclass can only represent one role.

Page 54: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--An Attempt to Fix the Inheritance Hierarchy

Person

CrewMember Passenger TicketAgentCrewMember Passenger TicketAgent

CrewMemberAndTicketAgent

CrewMemberAndPassenger

TicketAgentAndPassenger

CrewMemberAndTicketAgentAndPassenger

Page 55: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A More Rational Solution using Composition

CrewMember TicketAgent Passenger

Uses Uses Uses0..1 0..1 0..1

Person

Uses Uses Uses

1 1

1

Note: Many authors refer to this as delegation.

Page 56: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

TicketAgentObject

PassengerObject

Object Encapsulation via Composition

PersonObject

PersonObject

Page 57: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

An Alternative Composition for the Roles Example

CrewMember TicketAgent Passenger

Uses Uses Uses0..1 0..1 0..1

Person

Uses Uses Uses

1 1

1

Page 58: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Object Encapsulation for Alternative Composition

PersonPerson

PassengerTicketAgent Flight

CrewTicketAgent

Page 59: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Versus Composition--Some Guidelines

• It is generally not a good idea to use inheritance for the following purposes:– To represent dynamically changing alternative roles of

a superclass

– To hide methods or attributes inherited from a – To hide methods or attributes inherited from a superclass (restriction)

– To implement a domain-specific class as a subclass of a utility class (specialization)

– Purely for purposes of obtaining functionality needed to implement the subclass (implementation inheritane)/

Page 60: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Potential Drawbacks of Composition (Delegation)

• There may be some minor performance penalty for invoking an operation across object boundaries as opposed to using an inherited method.inherited method.

• Composition can’t be used with partially abstract (uninstantiable) classes

• Composition does not, in and of itself, impose any disciplined structure on the design (but neither does a class hierarchy).

Page 61: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

A Critical Look at Inheritance

“You can choose your associates, “You can choose your associates, but you’re stuck with your

ancestors.”

Page 62: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance in OOD

• Inheritance is often held to be sacrosanct in OOD.

• Tendency for OO developers to gauge the success of their efforts by the complexity of their inheritance hierarchy.their inheritance hierarchy.

• It is interesting to note that inheritance hierarchy examples in OO texts seldom deal with software design problems.

Page 63: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance--The Reality• Inheritance is a complex issue.

– Many different types of inheritance relationships.

– Basic notions differ among OO languages

– Some controversial issues--e.g. multiple inheritance.

– Inheritance can break encapsulation.– Inheritance can break encapsulation.

– Poorly conceived inheritance relationships can frustrate system reliability, maintainability, and evolvability.

• Inheritance is neither inherently good or bad. It must be used in a disciplined manner.

Page 64: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance--A Simple Classification

• Subclassing– inheritance of implementation fragments/code

from a superclass.

• Interface Inheritance• Interface Inheritance– inheritance of contract fragments/interfaces.

Page 65: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

The Complexities of Subclassing

• Methods of a class may freely invoke each other.

• Subclasses may override inherited methods.

• Subclass methods may call methods of • Subclass methods may call methods of superclasses, including overridden superclass methods.

• This is actually a form of “callback” from subclass to superclass.

Page 66: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Issues Example

Text-text: Array of Char-used:Integer = 0-caret:Integer = 0

+max( ):Integer+length( ):Integer+write(pos:Integer, ch:Char)

+setCaret(pos:Integer)

-cacheX: Integer = 0-cacheY: Integer = 0

SimpleText

+write(pos:Integer, ch:Char)+delete(pos:Integer)+caretPos( ):Integer+setCaret(pos:Integer)posToXCoord(pos:Integer):IntegerposToYCoord(pos:Integer):IntegerposFromCoord(x:Integer,

y:Integer):Integer+type(Char)+rubout( )

+setCaret(pos:Integer)+posToXCoord(pos:Integer):Integer+posToYCoord(pos:Integer):Integer+posFromCoord(x:Integer,

y:Integer):Integer+hideCaret( )+showCaret( )

Page 67: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--Continuedabstract class Text {

.

.

.private int caret = 0;

.

.

class SimpleText extends Text {...

void setCaret(int pos) {int old = caretPos( );if (old != pos) {.

.void setCaret(int pos) {caret = pos;}

.

.

.}

if (old != pos) {hideCaret( );super.setCaret(pos);showCaret( );

}...

}

Page 68: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--ContinuedInteraction diagram resulting from call to method type of Text class:

Text SimpleText Display

typecaretPos

writesetCaretsetCaret

hideCaret

(update display)Super.setCaret

showCaret

setCaret

Page 69: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--ContinuedA new version of Text class that “breaks” the subclass SimpleText:

abstract class Text {...

void write (int pos, char ch) {int i;int i;for ( i = used; i > pos; i--)

text[i] = text[i-1];used = used + 1;if (caret >= pos)

caret = caret +1;text[pos] = ch;

}...

Page 70: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Issues--The Fragile Base Class Problem

• There is an implicit interface between a class and its ancestor classes (superclasses).– Syntactic aspect--Does a class need to be

recompiled due to purely syntactic changes recompiled due to purely syntactic changes among it superclasses?

– Semantic Aspect--How dependent is a subclass upon changes in the implementation of its superclasses?

Page 71: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Dealing With Class-Subclass Dependencies

• Specialization Interface– Interface between a class and its subclasses

– For Java and C++, the specialization interface consists of the public and protected interface of the superclass.

• Various methods have been proposed to control behavior across a specialization interface, but these are largely of theoretical interest.

Page 72: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Alternatives to Inheritance--Object Composition

• Object composition--composition of behavior based upon references among objects rather than inheritance relations.

• Based upon “part of” relationship among • Based upon “part of” relationship among objects.– Suppose object A requests help from object B

– B is “part of” A if references to B do not leave A.

Page 73: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Object Composition

B

innerouter(owning)

CA(owned)objects

(owning)object

Note: A reuses the implementation of objects B and C

Page 74: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Composition Versus Inheritance

• An instantiated object has one notion of self even though it may inherit parts of its implementation from several superclasses.

• “Self-recursive” invocations of methods always return to the overriding version in always return to the overriding version in the lowest level subclass

• Composed objects do not have a common self—i.e.outer object does not share identity with inner objects.

Page 75: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example of self-recursive calls

BaseA( )

Sub

Base SubSubSub

A( )

uses A( )

B( )

super.A( )super.A( )usesSub

A( )

SubSubA( )

super.A( )usessuper.A( )

usessuper.A

Page 76: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example of Composition

BaseA”( )B( )

uses

:Base:SubSub :Sub

AA’

A’’

SubA’( )

SubSubA( )

uses

uses

Page 77: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Composition--Additional Observations• Composition requires that object

interactions, including recursive interactions among objects, be explicitly designed-in rather than an implicit by-product of implementation inheritance.implementation inheritance.

• Composition is a relationship between instantiated objects, not a relationship between classes.

• Composition can be made as general as subclassing by use of delegation.

Page 78: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance versus CompositionAn example of multiple inheritance from

a well-known OO textAPPLE PIE

APPLE PIE

Does this make sense?

Page 79: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Apple Pie Example-Continued

A more sensible approach

APPLE PIE

APPLE

1

*PIE SHELL

1

1

Page 80: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Apple Pie example--A Compositional Perspective

Apple Pie

Pie ShellApple

Page 81: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Versus Composition--Another Example

• Inheritance is generally not appropriate for “is a role played by” relationships.

• For instance, consider roles in an airline reservation system:reservation system:– passenger

– ticket agent

– flight crew

– etc.

Page 82: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A Potential Inheritance Hierarchy

Person

CrewMember TicketAgent Passenger

Is there a problem with this approach?

Page 83: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A Potential Inheritance Hierarchy

Person

CrewMember TicketAgent Passenger

Problem with this approach: a person may play different roles. An instantiated subclass can only represent one role.

Page 84: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--An Attempt to Fix the Inheritance Hierarchy

Person

CrewMember Passenger TicketAgentCrewMember Passenger TicketAgent

CrewMemberAndTicketAgent

CrewMemberAndPassenger

TicketAgentAndPassenger

CrewMemberAndTicketAgentAndPassenger

Page 85: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A More Rational Solution using Composition

CrewMember TicketAgent Passenger

Uses Uses Uses0..1 0..1 0..1

Person

Uses Uses Uses

1 1

1

Note: Many authors refer to this as delegation.

Page 86: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

TicketAgentObject

PassengerObject

Object Encapsulation via Composition

PersonObject

PersonObject

Page 87: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

An Alternative Composition for the Roles Example

CrewMember TicketAgent Passenger

Uses Uses Uses0..1 0..1 0..1

Person

Uses Uses Uses

1 1

1

Page 88: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Object Encapsulation for Alternative Composition

PersonPerson

PassengerTicketAgent Flight

CrewTicketAgent

Page 89: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Versus Composition--Some Guidelines

• It is generally not a good idea to use inheritance for the following purposes:– To represent dynamically changing alternative roles of

a superclass

– To hide methods or attributes inherited from a – To hide methods or attributes inherited from a superclass (restriction)

– To implement a domain-specific class as a subclass of a utility class (specialization)

– Purely for purposes of obtaining functionality needed to implement the subclass (implementation inheritane)/

Page 90: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Potential Drawbacks of Composition (Delegation)

• There may be some minor performance penalty for invoking an operation across object boundaries as opposed to using an inherited method.inherited method.

• Composition can’t be used with partially abstract (uninstantiable) classes

• Composition does not, in and of itself, impose any disciplined structure on the design (but neither does a class hierarchy).

Page 91: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

A Critical Look at Inheritance

“You can choose your associates, “You can choose your associates, but you’re stuck with your

ancestors.”

Page 92: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance in OOD

• Inheritance is often held to be sacrosanct in OOD.

• Tendency for OO developers to gauge the success of their efforts by the complexity of their inheritance hierarchy.their inheritance hierarchy.

• It is interesting to note that inheritance hierarchy examples in OO texts seldom deal with software design problems.

Page 93: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance--The Reality• Inheritance is a complex issue.

– Many different types of inheritance relationships.

– Basic notions differ among OO languages

– Some controversial issues--e.g. multiple inheritance.

– Inheritance can break encapsulation.– Inheritance can break encapsulation.

– Poorly conceived inheritance relationships can frustrate system reliability, maintainability, and evolvability.

• Inheritance is neither inherently good or bad. It must be used in a disciplined manner.

Page 94: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance--A Simple Classification

• Subclassing– inheritance of implementation fragments/code

from a superclass.

• Interface Inheritance• Interface Inheritance– inheritance of contract fragments/interfaces.

Page 95: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

The Complexities of Subclassing

• Methods of a class may freely invoke each other.

• Subclasses may override inherited methods.

• Subclass methods may call methods of • Subclass methods may call methods of superclasses, including overridden superclass methods.

• This is actually a form of “callback” from subclass to superclass.

Page 96: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Issues Example

Text-text: Array of Char-used:Integer = 0-caret:Integer = 0

+max( ):Integer+length( ):Integer+write(pos:Integer, ch:Char)

+setCaret(pos:Integer)

-cacheX: Integer = 0-cacheY: Integer = 0

SimpleText

+write(pos:Integer, ch:Char)+delete(pos:Integer)+caretPos( ):Integer+setCaret(pos:Integer)posToXCoord(pos:Integer):IntegerposToYCoord(pos:Integer):IntegerposFromCoord(x:Integer,

y:Integer):Integer+type(Char)+rubout( )

+setCaret(pos:Integer)+posToXCoord(pos:Integer):Integer+posToYCoord(pos:Integer):Integer+posFromCoord(x:Integer,

y:Integer):Integer+hideCaret( )+showCaret( )

Page 97: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--Continuedabstract class Text {

.

.

.private int caret = 0;

.

.

class SimpleText extends Text {...

void setCaret(int pos) {int old = caretPos( );if (old != pos) {.

.void setCaret(int pos) {caret = pos;}

.

.

.}

if (old != pos) {hideCaret( );super.setCaret(pos);showCaret( );

}...

}

Page 98: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--ContinuedInteraction diagram resulting from call to method type of Text class:

Text SimpleText Display

typecaretPos

writesetCaretsetCaret

hideCaret

(update display)Super.setCaret

showCaret

setCaret

Page 99: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example--ContinuedA new version of Text class that “breaks” the subclass SimpleText:

abstract class Text {...

void write (int pos, char ch) {int i;int i;for ( i = used; i > pos; i--)

text[i] = text[i-1];used = used + 1;if (caret >= pos)

caret = caret +1;text[pos] = ch;

}...

Page 100: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Issues--The Fragile Base Class Problem

• There is an implicit interface between a class and its ancestor classes (superclasses).– Syntactic aspect--Does a class need to be

recompiled due to purely syntactic changes recompiled due to purely syntactic changes among it superclasses?

– Semantic Aspect--How dependent is a subclass upon changes in the implementation of its superclasses?

Page 101: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Dealing With Class-Subclass Dependencies

• Specialization Interface– Interface between a class and its subclasses

– For Java and C++, the specialization interface consists of the public and protected interface of the superclass.

• Various methods have been proposed to control behavior across a specialization interface, but these are largely of theoretical interest.

Page 102: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Alternatives to Inheritance--Object Composition

• Object composition--composition of behavior based upon references among objects rather than inheritance relations.

• Based upon “part of” relationship among • Based upon “part of” relationship among objects.– Suppose object A requests help from object B

– B is “part of” A if references to B do not leave A.

Page 103: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Object Composition

B

innerouter(owning)

CA(owned)objects

(owning)object

Note: A reuses the implementation of objects B and C

Page 104: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Composition Versus Inheritance

• An instantiated object has one notion of self even though it may inherit parts of its implementation from several superclasses.

• “Self-recursive” invocations of methods always return to the overriding version in always return to the overriding version in the lowest level subclass

• Composed objects do not have a common self—i.e.outer object does not share identity with inner objects.

Page 105: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example of self-recursive calls

BaseA( )

Sub

Base SubSubSub

A( )

uses A( )

B( )

super.A( )super.A( )usesSub

A( )

SubSubA( )

super.A( )usessuper.A( )

usessuper.A

Page 106: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Example of Composition

BaseA”( )B( )

uses

:Base:SubSub :Sub

AA’

A’’

SubA’( )

SubSubA( )

uses

uses

Page 107: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Composition--Additional Observations• Composition requires that object

interactions, including recursive interactions among objects, be explicitly designed-in rather than an implicit by-product of implementation inheritance.implementation inheritance.

• Composition is a relationship between instantiated objects, not a relationship between classes.

• Composition can be made as general as subclassing by use of delegation.

Page 108: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance versus CompositionAn example of multiple inheritance from

a well-known OO textAPPLE PIE

APPLE PIE

Does this make sense?

Page 109: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Apple Pie Example-Continued

A more sensible approach

APPLE PIE

APPLE

1

*PIE SHELL

1

1

Page 110: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Apple Pie example--A Compositional Perspective

Apple Pie

Pie ShellApple

Page 111: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Versus Composition--Another Example

• Inheritance is generally not appropriate for “is a role played by” relationships.

• For instance, consider roles in an airline reservation system:reservation system:– passenger

– ticket agent

– flight crew

– etc.

Page 112: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A Potential Inheritance Hierarchy

Person

CrewMember TicketAgent Passenger

Is there a problem with this approach?

Page 113: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A Potential Inheritance Hierarchy

Person

CrewMember TicketAgent Passenger

Problem with this approach: a person may play different roles. An instantiated subclass can only represent one role.

Page 114: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--An Attempt to Fix the Inheritance Hierarchy

Person

CrewMember Passenger TicketAgentCrewMember Passenger TicketAgent

CrewMemberAndTicketAgent

CrewMemberAndPassenger

TicketAgentAndPassenger

CrewMemberAndTicketAgentAndPassenger

Page 115: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Roles Example--A More Rational Solution using Composition

CrewMember TicketAgent Passenger

Uses Uses Uses0..1 0..1 0..1

Person

Uses Uses Uses

1 1

1

Note: Many authors refer to this as delegation.

Page 116: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

TicketAgentObject

PassengerObject

Object Encapsulation via Composition

PersonObject

PersonObject

Page 117: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

An Alternative Composition for the Roles Example

CrewMember TicketAgent Passenger

Uses Uses Uses0..1 0..1 0..1

Person

Uses Uses Uses

1 1

1

Page 118: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Object Encapsulation for Alternative Composition

PersonPerson

PassengerTicketAgent Flight

CrewTicketAgent

Page 119: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Inheritance Versus Composition--Some Guidelines

• It is generally not a good idea to use inheritance for the following purposes:– To represent dynamically changing alternative roles of

a superclass

– To hide methods or attributes inherited from a – To hide methods or attributes inherited from a superclass (restriction)

– To implement a domain-specific class as a subclass of a utility class (specialization)

– Purely for purposes of obtaining functionality needed to implement the subclass (implementation inheritane)/

Page 120: A Critical Look at Inheritance - University of Iowauser.engineering.uiowa.edu/~hpca/LectureNotes/Slides3... · 2008-02-13 · A Critical Look at Inheritance “You can choose your

Potential Drawbacks of Composition (Delegation)

• There may be some minor performance penalty for invoking an operation across object boundaries as opposed to using an inherited method.inherited method.

• Composition can’t be used with partially abstract (uninstantiable) classes

• Composition does not, in and of itself, impose any disciplined structure on the design (but neither does a class hierarchy).