3

My question is rather theoretical:

Lets say I have Java 8 project using static and default methods in interfaces. I need to get rid of them because I am porting the code to Java 7 (lets say it is an Android app prior to Android N).

How to do that? I know there is Retrolambda plugin for that and I successfully use it. But I am thinking about "pure" solution done by altering the code. What are the options?

I know I can remove static and default methods from interface and put them into abstract class that implements this interface and then alter the code wherever there is reference to that interface (this is what Retrolambda does, if I understood the process correctly).

My teacher had indicated it can be done just by "suitable change in interface definition" without using additional class. But so far I failed in finding the solution. I tried putting implementations directly into target classes but that caused a series of troubles in my particular project.

Anyone have some idea or clue I am missing?

duffymo
  • 305,152
  • 44
  • 369
  • 561
Ellrohir
  • 1,017
  • 1
  • 14
  • 32
  • Static methods you can move anywhere, you can create a utility class for them for example. Default methods are usually a tool for interface evolution, so you don't really need them, just push the method body down to the actual classes implementing the interface. This would cause some problems with lambdas, but since you can't have lambdas anyway, happy days. :) – biziclop May 27 '16 at 09:27
  • Replacing the interfaces with abstract classes will not work if any class implements more than one of these interfaces. – Frank Puffer May 27 '16 at 09:27
  • 1
    Just re-ask your teacher about the "suitable change in interface definition" :) – AhmadWabbi May 27 '16 at 09:29
  • Well, by removing the `default` keyword you will force implementors to actually provide an implementation, i.e. copy the default implementation to those classes. – Kayaman May 27 '16 at 09:49

1 Answers1

3

I think it is at first questionable that you actually used such a thing in your Java8 project. Interface should remain without implementations. Default methods were introduced mainly for APIs backward compatibility(if method has to be added to Interface from previous versions and you can't afford to force users of API to change their code and you don't like creating InterfaceV2). Not for "daily usage".

I think when porting, you should just export the methods to the static *Util classes. This way you can reuse it and you are not breaking the main principle of Interface.

Javo
  • 435
  • 1
  • 5
  • 16
  • 2
    I wouldn’t say that `default` methods are “mainly for APIs backward compatibility”. Look, how many `default` methods are in the newly introduced interfaces of Java 8. Consider [this answer](http://stackoverflow.com/a/19998622/2711488), which explains why methods like `Predicate.negate` have a `default` implementation, but there are also methods like `Iterator.remove()` which *thankfully* now have a `default` implementation, despite not being new at all. – Holger May 27 '16 at 10:10
  • well...explain that to author of the source project :D – Ellrohir May 27 '16 at 12:10