1

I am having doubt with Java 8's default method feature with interfaces. Means with java 8 you can provide default implementation to a method in interface, so that it is no longer necessary for implementing classes to override that method.

Now interface is also able to contain abstract method as well as concrete method same as abstract class. Both behaving in same manner. Talking about the differences between interface and abstract methods can be identified like - 1. multiple interfaces can be implemented for a class, but only one class can be extended. 2. all interface fields are public static final, while in case of abstract class you have control over access modifiers of fields and methods.

My doubt is where exactly the difference comes into picture, that whether we should go for Abstract class or interface? What is the actual purpose having both Abstract class and interface separately?

gtm.r
  • 101
  • 1
  • 3

2 Answers2

1

In java 8 the differences between abstract class and interface have become more narrow. But there still remains differences between them. I will try to point out those differences. If I miss anything please let me know.

  1. You can create property with any kind of encapsulation(public, private or protected) and state(final or non-final) with abstract class. But the property in interface has to be public and final.
  2. You can only extend one abstract class but implements more than one interfaces.
  3. You can create constructor in abstract class but not in interface.
  4. You can associate state with abstract class where interface is preferred for stateless situations.

You can find oracle doc here on this topic.

Virtual extension methods/ Default Methods/Defender Methods of interface are the main reason of this narrow difference. So I will try to describe the necessity of introducing these types of methods in Java 8.

In java 8 lambda has been introduced. To take the full advantage of the lambda collection framework has been updated. forEach method has been added in Iterable interface. It was not feasible to change the interface without breaking the implementation. Default Method is used to add the functionality without breaking the implementation.

@FunctionalInterface
public interface Iterable<T> {
    Iterator<T> iterator();

    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }
}

I have added the code snapshot from here to demonstrate the usability of default method. So I think you have to choose between abstract class and interface based on your needs.

Diptopol Dam
  • 823
  • 1
  • 10
  • 39
0

Interface is fully abstract class. Java need both of them. You can find some good information here. Difference between an interface and an abstract class

Also you can see this question. What is the difference between an interface and abstract class

Community
  • 1
  • 1
Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48