Java 8: virtual extension methods vs abstract class

12,170

Solution 1

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.

Solution 2

  1. Abstract classes cannot be root classes of lambda expressions, while interfaces with virtual extension methods can be.
  2. Abstract classes can have constructors and member variables, while interfaces cannot. I believe its the execution of a possible constructor, and the possible throwing of a checked exception that prohibits abstract classes from being the root of a lambda expression.

If you want to write an API that allows the user to use lambda expressions, you should use interfaces instead.

Solution 3

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.

Share:
12,170
Kong
Author by

Kong

I'm a big hairy gorilla.

Updated on June 05, 2022

Comments

  • Kong
    Kong almost 2 years

    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?

    • assylias
      assylias almost 11 years
      lambdafaq.org/… - the distinction between inheritance of behaviour vs. inheritance of state is crucial.
    • jason
      jason almost 11 years
      And What are default methods? which pretty much lays out exactly their purpose. Their main purpose is to enable backwards compatibility with code that compiles against Java 7 and wouldn't implement any of the "functional" methods added to Java 8.
  • Brett
    Brett almost 11 years
    java.util.AbstractCollection? It's normal for abstract classes not to have state. From Java SE 8, the fact that abstract classes can have instance state and interfaces with implementations cannot is just a language oddity.
  • user541686
    user541686 almost 11 years
    @TomHawtin-tackline: But if you look at the documentation for AbstractCollection, it says, "This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface." In other words, it's just a convenience feature, but it doesn't need to be there for any particular design reasons. By contrast, Collection is necessary for writing generic code that works for all collections.
  • Brett
    Brett almost 11 years
    I don't think that's relevant. It is true that abstract class often do not hold state. In Java SE 8 I believe Collection will contain some "skeletal implementation of the Collection interface"
  • user541686
    user541686 almost 11 years
    @TomHawtin-tackline: "It is true that abstract class often do not hold state."... would you mind naming some other such classes?