Skip to main content
added 255 characters in body
Source Link
radai
  • 24.3k
  • 10
  • 76
  • 118

compareTo and inheritence is generally a tricky business. in theory, having subclasses to a Comparable concrete superclass breaks the comparable contract. for example - suppose i have a superclass A that compares to other A's by checking fields a1 and a1. now lets add a subclass B that has an extra field b1. anA.compareTo(aB) would compare by a1 and a2 while ((A)aB).compareTo(anA) would not do the same
as to your question - you can call the Animal version of compareTo only from the subclass itself, by doing something like

super.compareTo(<something>) 

but not from "outside", as you've overridden the method. it is your responsibility to maintain the same "contract" to the outside world as the method you've just overridden.

compareTo and inheritence is generally a tricky business. in theory, having subclasses to a Comparable concrete superclass breaks the comparable contract.
as to your question - you can call the Animal version of compareTo only from the subclass itself, by doing something like

super.compareTo(<something>) 

but not from "outside", as you've overridden the method. it is your responsibility to maintain the same "contract" to the outside world as the method you've just overridden.

compareTo and inheritence is generally a tricky business. in theory, having subclasses to a Comparable concrete superclass breaks the comparable contract. for example - suppose i have a superclass A that compares to other A's by checking fields a1 and a1. now lets add a subclass B that has an extra field b1. anA.compareTo(aB) would compare by a1 and a2 while ((A)aB).compareTo(anA) would not do the same
as to your question - you can call the Animal version of compareTo only from the subclass itself, by doing something like

super.compareTo(<something>) 

but not from "outside", as you've overridden the method. it is your responsibility to maintain the same "contract" to the outside world as the method you've just overridden.

Source Link
radai
  • 24.3k
  • 10
  • 76
  • 118

compareTo and inheritence is generally a tricky business. in theory, having subclasses to a Comparable concrete superclass breaks the comparable contract.
as to your question - you can call the Animal version of compareTo only from the subclass itself, by doing something like

super.compareTo(<something>) 

but not from "outside", as you've overridden the method. it is your responsibility to maintain the same "contract" to the outside world as the method you've just overridden.