0

In a Java Spring API, I'm implementing GeoJson Conversion Service to convert different types to geojson, I have GeoJsonConversionService interface, and one implementation is FileGeoJsonConversionServiceImpl class

First question: should the service name indicates the "action" or the "actor", which means should it named ConversionService or ConverterService.

Second: I found 2 examples of a conversion functionality, one used method overloading which is Jackson Library, and the other used method overriding, which is ResourceHttpMessageConverter

in Jackson, you have class ObjectMapper with different readTree methods, based on which type you convert to json, so you had readTree(InputStream in) and readTree(byte[] content) and so on.

in MessageConverter spring class, you have AbstractHttpMessageConverter abstract class, implemented by many classes, based on the conversion type also, such as ResourceHttpMessageConverter and StringHttpMessageConverter, all implementations implements readInternal function which is method overriding

so when to use method overloading over overriding?

1 Answer 1

0

I think the crux of your question is, when should you use different types of polymorphism in this case:

  • Subtype polymorphism (Overriding, Implementing an interface, etc)
  • Parametric polymorphism (Overloading)

You would typically use Subtype polymorphism when you want the client code to interact through a consistent interface (the implementation could change but the client doesn't need to). For example I have a String that I want to convert to a Java object, but as the client I don't care if its XML or JSON as long as I have a convertor that takes a String and gives me the Java object back.

Parametric polymorphism is typically used when the same action could be applied to different types of object for example if I have a JSON document that I need to convert to a Java object, however the JSON object might be a File, String or Byte[] (Byte array), in all cases the input is the JSON, but by overloading the method I don't force the caller to do a conversion first.

Summary:

  • Subtype polymorphism - provides a consistent interface, but often provides different behavior.
  • Parametric polymorphism - supports many datatypes, but usually provide similar behavior.

Note: These concepts are not mutually exclusive in that I could define an interface with methods that take File, String or Byte[] and then provide implementations for both XML and JSON that implement all three methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.