polymorphism what is it, why it is needed? dynamic binding sorting and searching

11
Polymorphism Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

Upload: gervase-floyd

Post on 18-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

PolymorphismPolymorphism

What is it, why it is needed?

Dynamic binding

Sorting and searching

Page 2: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

ChessPiece bishop;ChessPiece bishop;

bishop

Object : instance of ChessPiece

““having many forms”having many forms”Polymorphic reference can refer to different Polymorphic reference can refer to different types of objects at different points in timetypes of objects at different points in time

Page 3: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

Obj.doIt(); //if obj is polymorphic

In the loop

In a method

Commitment is made to execute certain code to carry out a method invocation : binding a method invocation to a method definition

Compile time binding vs. run time binding

polymorphic reference

late binding / dynamic binding

via inheritance / interface

Page 4: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

Polymorphism via InheritancePolymorphism via Inheritance

Class name reference variable;Reference variable can refer to any object of that class

related to its declared type by inheritance

mammal pet;

horse secretariat = new Horse();

pet = secretariat;

animal creature = new Horse();

creature.move();creature.move();

Object reference?

Page 5: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

Firm has 3 different staff members

Volunteer, Employee( Executive, Hourly)

Employee: name address phone

Employee: + ssn payrate

Executive: + bonus

Houlry = - bonus + hours worked

Page 6: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

Firm

Main():void

Staff

StaffList: StaffMember[]

Payday(): void

StaffMember

Name: StringAddress: StrinhPhone: String

toString(): StringPay(): double

Volunteer

pay(): double

employee

Ssn:StringpatRate: double

toString():StringPay:double

ExecutiveM

Bonus:double

Awardbonus():voidPay():double

Hourly

hoursWorked: int

Addhours():voidPay(): double

toString() : string

Page 7: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

3

9

1

6

2

1 1

2

1

2

3

6

1

2

3

6

1

2

3

9

Selection

Insertion

quick

Page 8: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

3

9

1

6

2

3

9

1

6

2

1

9

3

6

2

1

9

3

6

2

1

2

3

6

9

1

2

3

6

9

1

2

6

3

9

1

2

6

3

9

Page 9: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

3

9

1

6

2

temp = myarray[2];

myarray[2] = myarray[1];

myarray[1] = temp;

3

6

1

9

2

temp = myarray[3];

Myarray[1]=myarray[0];

myarray[2] = myarray[1];

myarray[3] = myarray[2];

Myarray[0] = temp

Page 10: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching

Sort and search- Insertion, selection, quick, merge

- Linear search, binary search

Page 11: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching