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?

4
  • 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. :) Commented May 27, 2016 at 9:27
  • Replacing the interfaces with abstract classes will not work if any class implements more than one of these interfaces. Commented May 27, 2016 at 9:27
  • 1
    Just re-ask your teacher about the "suitable change in interface definition" :) Commented May 27, 2016 at 9: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. Commented May 27, 2016 at 9:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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, 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.
well...explain that to author of the source project :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.