NottmTony
NottmTony

Reputation: 476

java 7 alternative to default methods in interface (only in Java 8)

I wanted to use default methods in one of my interface classes - then I realised that this is only available in Java 8 - and we are using Java 7.

What is the best way of achieving similar in Java 7 ?

Upvotes: 5

Views: 7688

Answers (2)

Aaron
Aaron

Reputation: 24812

Have your methods signature in an interface, as in Java 8.
Have your method default implementation in an abstract class implementing that interface.
Have your method final implementation in a class extending that abstract class.

Now you have to be aware that it will still not be the same as having Java 8's interfaces' default method, in particular because you cannot extend multiple abstract class, while you can implement multiple interfaces with their default methods.

Upvotes: 5

Nikesh Joshi
Nikesh Joshi

Reputation: 825

You have only one option for that in java 7 , use abstract class.

Upvotes: 0

Related Questions