Introduction to Polymorphism - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Introduction to Polymorphism

Description:

... different types of cars with different features, but each has a price and quantity in stock. ... car. motorcycle. truck. transporter. semi. car. car. car. car ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 15
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Polymorphism


1
Introduction to Polymorphism
2
Outline
  • Prerequisites
  • Functions with Memory
  • Local
  • Basic OO in Scheme
  • Objectives
  • Understand Polymorphism

3
Background
  • We set out to explore the Object Oriented
    Paradigm.
  • We claimed that it would allow us to have
    superior
  • Encapsulation
  • Reusability
  • Adaptability
  • But as of now that may not be quite so apparent.
  • A list might hold a collection of Student
    Records, but to hold anything else it will
    require some a significant amount of modification
  • We need another tool to allow us to make
    collections that will hold anything!

4
Scenarios
  • A veterinarian's algorithm might have a list of
    animals, but each one needs different food or
    care we want ONE information system to track all
    of this without complex logic for each individual
    kind of animal.
  • A car dealership sells many different types of
    cars with different features, but each has a
    price and quantity in stock.
  • A registration system might treat in-state
    students differently from out-of-state students,
    graduate students differently from
    undergraduates, etc.

5
Motivation
  • Wed like to be able to manage different kinds of
    classes.
  • Since different classes often share common
    methods and attributes, wed like to make use of
    this fact to make our algorithms simpler.

6
Polymorphism
  • Polymorphism means taking many or multiple forms.
  • Refers to the ability to tell an object to
    perform a method without knowing exactly how that
    method is going to get invoked.
  • May initially seem somewhat magical
  • Well, actually it is...

7
Processing Collections
  • One of the main benefits of polymorphism is the
    ability to easily process collections with common
    methods.
  • We will consider a collection (list) of vehicles
    in the next example. . .

8
Different Vehicles
car
motorcycle
truck
semi
transporter
car
car
car
car
car
9
A Collection of Vehicles
  • Imagine a simulation needs to manipulate all of
    the different types of vehicles.
  • Rather than maintain multiple separate lists, one
    each for cars, motorcycles, trucks, semis,
    transporters etc
  • We can maintain only one list of vehicles as long
    as they all share the appropriate methods.

10
Polymorphic Vehicles
  • the vehicle list is polymorphic
  • It is holding vehicles of many forms.
  • Each of the vehicles is within the family of
    vehicles because they share the same method
    names.
  • Each one may actually implement the methods
    differently.

11
polymorphism.scm
12
Summary
  • Polymorphism allows objects to represent
    instances of its own class and any of its
    subclasses.
  • Polymorphic collections are useful for managing
    objects since all objects are descendants of
    class Object
  • No matter what the reference type the actual
    object knows what type it is
  • When there is a choice dynamic binding goes to
    the lowest level.

13
Questions?
14
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com