week 3 part 3 polymorphism - nathaniel g. martin

12
Object Oriented Programming Week 3 Part 3 Polymorphism

Upload: others

Post on 05-Jan-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Object Oriented Programming

Week 3 Part 3Polymorphism

Page 2: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 2

Lecture

● What is Polymorphism

● Examples of Polymorphism

Page 3: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 3

What is Polymorphism?

Page 4: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 4

Polymorphism

● Polymorphism lets programs use the samename for different functions.– It comes from the Greek poly (many) morph (form)

– The name was adapted from biology where itmeans the same animal with different forms

Page 5: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 5

Types of Polymorphism

● There are three types of Polymorphism– Ad Hoc Polymorphism: the meaning of the

function is determined by the type of a functionsparameters and its return type.

– Sub-typing Polymorphism: the meaning of thefunction is determined by the type of the object onwhich is is called

– Parametric Polymorphism: writing a function ormethod generically so it can manage different types.

● These are called Generic and will be dealt with later.

Page 6: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 6

Ad Hoc Polymorphism

● The simplest type of Polymorphism determinesthe behavior of a function or operator based onits operands– E.g. Addition of ints in the sum of the two values;

additions of two strings is their concatenation● 1 + 2 == 3; “1” + “2” == “12”

– We may use the same method name for differentfunctions if the parameters are different.

– We may use this do provide different Constructors:

Page 7: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 7

Sub-type Polymorphism

● Subclasses inherit methods, which will behavethe same way regardless of the subclass.

● Subclasses may override methods, whichcauses them to behave differently.

● The behavior is determined by the type.

● We have seen this in our Test file.

Page 8: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 8

Sub-type Polymorphism: Test

Types defined here

d.say()

b.say()

c.say()

m.say()

a.say()

Page 9: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 9

Test Output

a.says()

m.says()

d.says()

b.says()

c.says()

Each of the says() method produces a different output based on the type for whichit is a method.

Page 10: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 10

Subtype Polymorphism without vars

● This is not that different from ad hoc polymorphism– You could think of the variables against which the method

is being called as another parameters

● However, you do not need to have a variable of thattype.– Subclasses can be assigned to arrays of its superclass

– The type of the object in the array determines the actionof the method, not the type of the list

Page 11: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 11

Advanced Subtype Polymorphism

List of Animals

Initialized to different types

Constructor called

Elements of list call says()

Page 12: Week 3 Part 3 Polymorphism - Nathaniel G. Martin

Week 3 12

PolymorphismDemo Output

animalList[0] is an Animal

animalList[1] is a Mammal

animalList[3] is a Bird

animalList[4] is a Crow

animalList[2] is a Dog

Each element of the list calls the appropriate says() method.