Java 8: virtual extension methods vs abstract clas

2019-02-07 18:00发布

问题:

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?

回答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.



回答2:

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.



回答3:

  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.



回答4:

Abstract classes scores over java-8 interfaces in below areas.

  1. With abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public

  2. Mutable state can be shared/modified with child classes unlike interface which have only constants

  3. Abstract class can be used to implement Template_method_pattern : It defines the program skeleton of an algorithm in an operation, deferring some steps to sub-classes.
  4. Abstract class can be used to implement Decorator_pattern: A design pattern that allows behaviuor to be added to an individual object, either statically or dynamically, without affecting the behaviour of other objects from the same class.


回答5:

Difference between Abstract class & functional interface are many like all the difference between a normal interface and abstract class but measure difference is we can have default methods in functional interface but not in abstract class, this changes and helped all the collection implementation in java 8 foreach() and other performance method with use of lambda

package com.akhi;
public abstract class AbstractDemo {
abstract void letsRun(); // abstract valid
public String toString(); // invalid but valid in interface or functional interface

public boolean equals(Object o); // invalid but valid in interface or functional interface

public int concrete() { // Concrete is invalid in interface
    return 1;
}

public default int getMult(int a, int b) // default invalid but valid in case of functional
{
    return a * b;
}

public static int getSum(int a, int b) // static allowed
{
    return a + b;
}
}