polymorphism what is it, why it is needed? dynamic binding sorting and searching
TRANSCRIPT
PolymorphismPolymorphism
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
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
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?
Firm has 3 different staff members
Volunteer, Employee( Executive, Hourly)
Employee: name address phone
Employee: + ssn payrate
Executive: + bonus
Houlry = - bonus + hours worked
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
3
9
1
6
2
1 1
2
1
2
3
6
1
2
3
6
1
2
3
9
Selection
Insertion
quick
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
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
Sort and search- Insertion, selection, quick, merge
- Linear search, binary search