Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: virtual extension methods vs abstract class

I'm looking at the new virtual extension methods in Java 8 interfaces:

public interface MyInterface {    default String myMethod() {        return "myImplementation";     } } 

I get their purpose in allowing an interface to evolve over time, and the multiple inheritance bit, but they look awfully like an abstract class to me.

If you're doing new work are abstract classes prefered over extension methods to provide implementation to an "interface" or are these two approaches conceptually equivalent?

like image 251
Kong Avatar asked Aug 12 '13 23:08

Kong


People also ask

Is there any difference between abstract class and interface from Java 8 onwards?

Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can't have any method implementations. Note that from Java 8 onwards, we can create default and static methods in interface that contains the method implementations.

Do we still need abstract class in Java 8?

After Java 8, an interface can have default and static methods along with abstract methods. Interfaces don't support final methods. But, abstract classes support final as well as non-final methods and static as well as non-static methods along with abstract methods.

When should I use interface and abstract class in Java 8?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.

What are the main differences between an interface with default method and an abstract class in Java 8?

An abstract class can override Object class methods, but an interface can't. An abstract class can declare instance variables, with all possible access modifiers, and they can be accessed in child classes. An interface can only have public, static, and final variables and can't have any instance variables.


2 Answers

One primary purpose of such constructs is to preserve backwards compatibility. The addition of closures to the Java language is quite a major alteration, and things need to be updated to fully take advantage of this. For example, Collection in Java 8 will have methods such as forEach() which work in conjunction with lambdas. Simply adding such methods to the pre-existing Collection interface would not be feasible, since it would break backwards compatibility. A class I wrote in Java 7 implementing Collection would no longer compile since it would lack these methods. Consequently, these methods are introduced with a "default" implementation. If you know Scala, you can see that Java interfaces are becoming more like Scala traits.

As for interfaces vs abstract classes, the two are still different in Java 8; you still can't have a constructor in an interface, for example. Hence, the two approaches are not "conceptually equivalent" per se. Abstract classes are more structured and can have a state associated with them, whereas interfaces can not. You should use whichever makes more sense in the context of your program, just like you would do in Java 7 and below.

like image 97
arshajii Avatar answered Sep 22 '22 15:09

arshajii


Abstract classes hold state (instance fields), in order to provide some common behavior (methods).
You don't typically (ever?) see an abstract class without state.

Interfaces specify functionality. They are meant to declare behavior as a contract, not implement it.
Thus any methods that are specified as part of an interface are "helper" methods -- they don't affect the implementation.

like image 20
user541686 Avatar answered Sep 24 '22 15:09

user541686