interface - Java 8: virtual extension methods vs abstract class -
i'm looking @ new virtual extension methods in java 8 interfaces:
public interface myinterface { default string mymethod() { return "myimplementation"; } }
i purpose in allowing interface evolve on time, , multiple inheritance bit, awfully abstract class me.
if you're doing new work abstract classes prefered on extension methods provide implementation "interface" or these 2 approaches conceptually equivalent?
one primary purpose of such constructs preserve backwards compatibility. addition of closures java language quite major alteration, , things need updated take advantage of this. example, collection
in java 8 have methods such foreach()
work in conjunction lambdas. adding such methods pre-existing collection
interface not feasible, since break backwards compatibility. class wrote in java 7 implementing collection
no longer compile since lack these methods. consequently, these methods introduced "default" implementation. if know scala, can see java interface
s becoming more scala trait
s.
as interfaces vs abstract classes, 2 still different in java 8; still can't have constructor in interface, example. hence, 2 approaches not "conceptually equivalent" per se. abstract classes more structured , can have state associated them, whereas interfaces can not. should use whichever makes more sense in context of program, in java 7 , below.
Comments
Post a Comment