Introduction Forms of Polymorphism Summary References
POLYMORPHISM AND SOFTWARE REUSE
Muhammad Adil Raja
Roaming Researche...
Introduction Forms of Polymorphism Summary References
OUTLINE I
INTRODUCTION
FORMS OF POLYMORPHISM
SUMMARY
REFERENCES
Introduction Forms of Polymorphism Summary References
INTRODUCTION
• In this chapter and the four that follow we will star...
Introduction Forms of Polymorphism Summary References
DEFINITION OF POLYMORPHISM
MESSAGE SYNTAX
• Polymorphous: Having, or...
Introduction Forms of Polymorphism Summary References
FORMS OF POLYMORPHISM IN OOP
• There are four major forms of polymor...
Introduction Forms of Polymorphism Summary References
TWO FORMS OF SOFTWARE REUSE
• One of the major goals of OOP is softw...
Introduction Forms of Polymorphism Summary References
EXAMPLE – BUILDING SETS FROM LISTS
• Suppose we have already a List ...
Introduction Forms of Polymorphism Summary References
USING INHERITANCE
Only need specify what is new - the addition metho...
Introduction Forms of Polymorphism Summary References
USING COMPOSITION
• Everything must be redefined, but implementation ...
Introduction Forms of Polymorphism Summary References
ADVANTAGES AND DISADVANTAGES OF EACH
MECHANISM
• Composition is simp...
Introduction Forms of Polymorphism Summary References
REFERENCES
• Images and content for developing these slides have bee...
of 11

Polymorphism and Software Reuse

A presentation about polymorphism and software reuse in the context of object-oriented programming.
Published on: Mar 4, 2016
Published in: Software      
Source: www.slideshare.net


Transcripts - Polymorphism and Software Reuse

  • 1. Introduction Forms of Polymorphism Summary References POLYMORPHISM AND SOFTWARE REUSE Muhammad Adil Raja Roaming Researchers, Inc. cbna April 20, 2015
  • 2. Introduction Forms of Polymorphism Summary References OUTLINE I INTRODUCTION FORMS OF POLYMORPHISM SUMMARY REFERENCES
  • 3. Introduction Forms of Polymorphism Summary References INTRODUCTION • In this chapter and the four that follow we will start to investigate polymorphism, as it is found in many forms in object-oriented languages. • In this chapter we will outline the four major types of polymorphism, then explore them in more detail in the later chapters. • The different mechanisms each have the same goal of encouraging software reuse, facilitating ease of understanding and speeding application development.
  • 4. Introduction Forms of Polymorphism Summary References DEFINITION OF POLYMORPHISM MESSAGE SYNTAX • Polymorphous: Having, or assuming, various forms, characters, or styles. • From greek routes, poly = many, and Morphos = form (Morphus was the greek god of sleep, who could assume many forms, and from which we derive the name Morphine, among other things). • A polymorphic compound can crystalize in many forms, such as carbon, which can be graphite, diamonds, or fullerenes. • In programming languages, used for a variety of different mechanisms. • Usage of the term is confused by the fact that it means something slightly different in the functional programming community than it does in the OO world.)
  • 5. Introduction Forms of Polymorphism Summary References FORMS OF POLYMORPHISM IN OOP • There are four major forms of polymorphism in object-oriented languages: • Overloading (ad hoc polymorphism) – one name that refers to two or more different implementations. • Overriding (inclusion polymorphism) – A child class redefining a method inherited from a parent class. • The Polymorphic Variable (assignment polymorphism) – A variable that can hold different types of values during the course of execution. • It is called Pure Polymorphism when a polymorphic variable is used as a parameter. • Generics (or Templates) – A way of creating general tools or classes by parameterizing on types.
  • 6. Introduction Forms of Polymorphism Summary References TWO FORMS OF SOFTWARE REUSE • One of the major goals of OOP is software reuse. • We can illustrate this by considering two different approaches to reuse: • Inheritance – the is-a relationship. • Composition – the has-a relationship. • We do this by considering an example problem that could use either mechanism.
  • 7. Introduction Forms of Polymorphism Summary References EXAMPLE – BUILDING SETS FROM LISTS • Suppose we have already a List data type with the following behavior: • Want to build the Set data type (elements are unique). LISTS class L i s t { public : void add ( int ) ; int includes ( int ) ; void remove ( int ) ; int firstElement ( ) ; } ;
  • 8. Introduction Forms of Polymorphism Summary References USING INHERITANCE Only need specify what is new - the addition method. Everything else is given for free. INHERITANCE class Set : public L i s t { public : void add ( int ) ; } ; void Set : : add ( int x ) { i f ( ! includes ( x ) ) / / only include i f not already there L i s t : : add ( x ) ; }
  • 9. Introduction Forms of Polymorphism Summary References USING COMPOSITION • Everything must be redefined, but implementation can make use of the list data structure. COMPOSITION class Set { public : void add ( int ) ; int includes ( int ) ; void remove ( int ) ; int firstElement ( ) ; private : L i s t data ; } ; void Set : : add ( int x ) { i f ( ! data . includes ( x ) ) data . add ( x ) ; } int Set : : includes ( int x ) { return data . includes ( x ) ; } void Set : : remove ( int x ) { data . remove ( x ) ; } int Set : : firstElement ( ) { return data . firstElement ( ) ; }
  • 10. Introduction Forms of Polymorphism Summary References ADVANTAGES AND DISADVANTAGES OF EACH MECHANISM • Composition is simpler, and clearly indicates what operations are provided. • Inheritance makes for shorter code, possibly increased functionality, but makes it more difficult to understand what behavior is being provided. • Inheritance may open to the door for unintended usage, by means of unintended inheritance of behavior. • Easier to change underlying details using composition (i.e., change the data representation). • Inheritance may permit polymorphism. • Understandability is a toss-up, each has different complexity issues (size versus inheritance tree depth). • Very small execution time advantage for inheritance.
  • 11. Introduction Forms of Polymorphism Summary References REFERENCES • Images and content for developing these slides have been taken from the follwoing book with the permission of the author. • An Introduction to Object Oriented Programming, Timothy Budd. • This presentation is developed using Beamer: • Singapore, beaver.

Related Documents