Inheritance, Polymorphism and Dynamic Binding - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Inheritance, Polymorphism and Dynamic Binding

Description:

'Kerry is a Border Collie which is a kind of a dog' Kerry is an instance of the class Border Collie which is a subclass of the Class ... – PowerPoint PPT presentation

Number of Views:142
Avg rating:3.0/5.0
Slides: 16
Provided by: cseD1
Category:

less

Transcript and Presenter's Notes

Title: Inheritance, Polymorphism and Dynamic Binding


1
Inheritance, Polymorphism and Dynamic Binding
2
Inheritance
  • Basically, inheritance defines a relationship
    among classes, wherein one class shares the
    structure or behaviour defined in one or more
    classes (called single inheritance and multiple
    inheritance, respectively). Booch
  • Inheritance is therefore an implementation
    construct

3
Inheritance and Human Reasoning
  • Inheritance is an implementation language
    construct that maps to cognitive processing
  • we think of new abstractions in terms of ones we
    already understand
  • In Analysis we look for Generalisation-Specialisat
    ion hierarchies which can be implemented with
    inheritance.

4
How to think about Inheritance
  • Inheritance is the Is-a-Kind-of relationship
  • Kerry is a Border Collie which is a kind of a
    dog
  • Kerry is an instance of the class Border Collie
    which is a subclass of the Class dog

5
Implications of Inheritance
  • We should use inheritance to extend the behaviour
    of an existing class

Bank Account
Credit ( ) Debit ( )
Savings Account
Interest( )
6
Design Principle Liskov Substitution
  • What is wanted here is something like the
    following substitution property If for each
    object o1 of type S there is a an object o2 of
    type T such that for all programs P in terms of
    T, the behavior of P is unchanged when o1 is
    substituted for o2 then S is a subtype of T
    Barbara Liskov Data abstraction and hierarchy,
    SIGPLAN Notices, May 1988
  • In plain English, only place a class in an
    inheritance hierarchy if one of its objects can
    be substituted for an object of its base class
    without changing the expected behavior of the
    program
  • Beware - this can be subtle!

7
Inheritance in a Simple Graphics System
  • Place the following classes in an inheritance
    hierarchy (The application is a graphics system
    in which shapes can be drawn, moved, rotated and
    stretched along a horizontal axis, or a vertical
    axis.)
  • Point, Line, Spline, Elliptical curve, Ellipse,
    Square, Graphical Item, Circle, Rectangle
  • Hint Add the abstract class ClosedShape to your
    hierarchy

8
A Solution?
ClosedShape
Line
Point
abstract
Where do Square, Rectangle, Circle and Ellipse go?
Hstretch(factor) Vstretch(factor)
9
Sidebar Why is Multiple Inheritance a Problem?
  • Answer It isnt - Repeated Inheritance is

Helicopter
Jet Plane
Fly ( )
Fly ( )
Helijet
10
Other problems with inheritance
  • Programming languages do not constrain you to
    inherit only on behaviour
  • Interface inheritance is not cleanly separated
    from implementation inheritance (except in Java)
  • The Yo-yo problem
  • Overuse of inheritance limits reuse!!
  • Often composition and delegation are a better
    choice for extending behaviour

11
Polymorphism
  • Literally many forms
  • In OO means that given the same message, two
    different (but usually related) classes will
    respond with different implementations of the
    same abstract behaviour
  • e.g., a circle class and a polygon class will use
    different implementations to draw themselves

12
Polymorphic Interfaces
  • Polymorphic interfaces are fundamental to the
    Object paradigm
  • Separation of interface and implementation
    enables Responsibility Driven Design
  • where objects are considered to take
    responsibility for implementing their own
    behaviour
  • Leads to easier maintenance
  • IF statement considered harmful!

13
Typed and Untyped Polymorphism
  • Typed polymorphism (due to classes being in the
    same inheritance hierarchy) leads to overriding
    of superclass methods by subclasses
  • e.g., Debit() method of SavingsAccount overrides
    the implementation in BankAccount
  • Untyped polymorphism is when classes share the
    same interface, but have no other relationship

14
Overloading versus Overriding
  • Do not confuse overloading with overriding
  • An overriden method means the same semantics are
    being implemented in different ways
  • An overloaded method means the same method name
    has two different meanings
  • usually has slightly different signature
  • but not always (remember MichaelAngelo and
    BillyTheKid?)

15
Dynamic Binding
  • Polymorphic methods are resolved at run time (a
    message is bound to an implementation of the
    method it invokes)
  • Permits the elimination of case-tests in client
    programs
  • Maximises the flexibility of the software
  • BUT it means the run-time structure of an OO
    program looks nothing like its static structure
    (debugging can be painful!)
Write a Comment
User Comments (0)
About PowerShow.com