Skip to main content
Commonmark migration
Source Link

#Conclusion

Conclusion

#Notes

Notes

Bonus

#Bonus ComparatorComparator is also interesting because it works around the issue of implementing equals() correctly. Better yet, it follows a pattern for fixing this type of inheritance issue: the Strategy design pattern. The Typeclasses that Haskell and Scala people get excited about are also the Strategy pattern. Inheritance isn't bad or wrong, it's just tricky. For further reading, check out Philip Wadler's paper How to make ad-hoc polymorphism less ad hoc

#Conclusion

#Notes

#Bonus Comparator is also interesting because it works around the issue of implementing equals() correctly. Better yet, it follows a pattern for fixing this type of inheritance issue: the Strategy design pattern. The Typeclasses that Haskell and Scala people get excited about are also the Strategy pattern. Inheritance isn't bad or wrong, it's just tricky. For further reading, check out Philip Wadler's paper How to make ad-hoc polymorphism less ad hoc

Conclusion

Notes

Bonus

Comparator is also interesting because it works around the issue of implementing equals() correctly. Better yet, it follows a pattern for fixing this type of inheritance issue: the Strategy design pattern. The Typeclasses that Haskell and Scala people get excited about are also the Strategy pattern. Inheritance isn't bad or wrong, it's just tricky. For further reading, check out Philip Wadler's paper How to make ad-hoc polymorphism less ad hoc

added 448 characters in body
Source Link
GlenPeterson
  • 14.9k
  • 7
  • 48
  • 75

Now let's sub-class it to be a 3D point:.

#Notes Josh Bloch's "Effective Java" Item 8 was the source of this example, but Bloch uses a ColorPoint which adds a color instead of a third axis and uses doubles instead of ints. Bloch's Java example is basically duplicated by Odersky/Spoon/Venners who made their example available online.

  1. Josh Bloch's "Effective Java" Item 8 was the source of this example, but Bloch uses a ColorPoint which adds a color instead of a third axis and uses doubles instead of ints. Bloch's Java example is basically duplicated by Odersky/Spoon/Venners who made their example available online.

  2. Several people have objected to this example because if you let the parent class know about the sub-class, you can fix this issue. That's true if there are a small enough number of sub-classes and if the parent knows about them all. But the original question was about making an API which someone else will write sub-classes for. In that case, you generally can't update the parent implementation to be compatible with the sub-classes.

Now let's sub-class it to be a 3D point:

#Notes Josh Bloch's "Effective Java" Item 8 was the source of this example, but Bloch uses a ColorPoint which adds a color instead of a third axis and uses doubles instead of ints. Bloch's Java example is basically duplicated by Odersky/Spoon/Venners who made their example available online.

Now let's sub-class it to be a 3D point.

#Notes

  1. Josh Bloch's "Effective Java" Item 8 was the source of this example, but Bloch uses a ColorPoint which adds a color instead of a third axis and uses doubles instead of ints. Bloch's Java example is basically duplicated by Odersky/Spoon/Venners who made their example available online.

  2. Several people have objected to this example because if you let the parent class know about the sub-class, you can fix this issue. That's true if there are a small enough number of sub-classes and if the parent knows about them all. But the original question was about making an API which someone else will write sub-classes for. In that case, you generally can't update the parent implementation to be compatible with the sub-classes.

added 163 characters in body
Source Link
GlenPeterson
  • 14.9k
  • 7
  • 48
  • 75

There are two morals of this story.#Conclusion

A great example of a well-spelled out contract is Comparator. Just ignore what it says about .equals() for the reasons described above. Here's an example of how Comparator can do things .equals() can't.

There are two morals of this story.

A great example of a well-spelled out contract is Comparator. Just ignore what it says about .equals() for the reasons described above.

#Conclusion

A great example of a well-spelled out contract is Comparator. Just ignore what it says about .equals() for the reasons described above. Here's an example of how Comparator can do things .equals() can't.

deleted 74 characters in body
Source Link
GlenPeterson
  • 14.9k
  • 7
  • 48
  • 75
Loading
added 90 characters in body
Source Link
GlenPeterson
  • 14.9k
  • 7
  • 48
  • 75
Loading
added 95 characters in body
Source Link
GlenPeterson
  • 14.9k
  • 7
  • 48
  • 75
Loading
added 70 characters in body
Source Link
GlenPeterson
  • 14.9k
  • 7
  • 48
  • 75
Loading
deleted 47 characters in body
Source Link
GlenPeterson
  • 14.9k
  • 7
  • 48
  • 75
Loading
Loading
added 57 characters in body
Source Link
GlenPeterson
  • 14.9k
  • 7
  • 48
  • 75
Loading
Source Link
GlenPeterson
  • 14.9k
  • 7
  • 48
  • 75
Loading