• Post Reply Bookmark Topic Watch Topic
  • New Topic

why is this not a compileation error?  RSS feed

 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based from :

http://docs.oracle.com/javase/tutorial/java/generics/boundedTypeParams.html:



from the example, countGreaterThan method invokes compareTo method from Comparable interface. However, it was not defined the implementation of compareTo. Why is this not a compilation error?
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism.

countGreaterThan() doesn't care about the specific implementation of the Comparable interface. It knows that arguments passed to it at runtime are concrete instances with defined implementations, because you can not instantiate abstract types.

Try to write code that calls countGreaterThan() without using concrete instances or null. You can't.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!