10.4 Summary
10.4 Summary
Natural Order (Python vs. Java)
Today we’ve seen polymorphism and function passing.
For comparing two objects using an intrinsic order (a.k.a. a natural order), Python uses a form of polymorphism called operator overloading. Java instead uses subtype polymorphism.
Note: Python is duck typed: Do not have to specify if > is available or not.
Other Orders (Python vs. Java)
For comparing two objects using an alternate order, Python uses function passing, i.e. you provide a key function. By contrast, Java uses subtype polymorphism, just like for intrinsic orders. We package the comparison function in a Comparator
object.
Writing Library Functions
If we want to write the code that actually uses a Comparable
or Comparator
, then we'll find the method specifications get a little vexing, for example:
Luckily for you, you won't have to deal with this much, other than at the end of Project 1B.
Last updated