polymorphism what is it, why it is needed? dynamic binding sorting and searching
TRANSCRIPT
![Page 1: Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082409/5697bfea1a28abf838cb7888/html5/thumbnails/11.jpg)