Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferences of abstract classes over interfaces in Java 8

Now, we know that Java 8 has introduced default and static methods in interfaces.
Interfaces were originally introduced in Java to avoid the diamond problem that occurred in C++, in multiple inheritance.

But along with the introduction of default methods in interfaces in Java 8, now, Java has also introduced the diamond problem, which it avoided in previous versions.

Default methods are not compulsorily needed to be overridden.
But when a diamond problemoccurs using interfaces, the class implementing those interfaces must override the default methods.

So now, I have three questions in my mind:

  1. Why is the need to have default methods?
  2. Couldn't we have multiple inheritance through classes itself, in place of having default methods in interfaces?
  3. And what was the need to avoid diamond problem in the previous versions, if they had to introduce it in Java 8 anyway?

Any good explanation or any link for explanation?

PS I did not find any link on the internet containing any good article on this.
All they said is that an abstract class gives you more concreteness.
As in, abstract classes can have constructors but interfaces cannot.

So again, I want to know, If abstract classes are more concrete, and can have constructors,
and anyways Java has introduced the diamond problem, why should we have interfaces now? Wouldn't abstract classes be good enough as a stand alone for multiple inheritance?

like image 574
Aditya Singh Avatar asked Jun 23 '14 11:06

Aditya Singh


People also ask

What is the diff between abstract class and interface in Java 8?

Type of methods: Interface can have only abstract methods. An abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also. Final Variables: Variables declared in a Java interface are by default final.

When an abstract class should be used in preference to an interface?

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.

What is the use of abstract class over interface?

An abstract class is used if you want to provide a common, implemented functionality among all the implementations of the component. Abstract classes will allow you to partially implement your class, whereas interfaces would have no implementation for any members whatsoever.

What is the main difference between interface and abstract class in Java?

Abstract class can inherit another class using extends keyword and implement an interface. Interface can inherit only an inteface. Abstract class can be inherited using extends keyword. Interface can only be implemented using implements keyword.


2 Answers

No, it didn't reintroduce the diamond problem, because interfaces still can't have any state, and default methods may not be final.

So, when you choose to implement two interfaces, you still have all the freedom you want to implement the default methods, either by choosing one of the provided default implementations, or by providing your own implementation. But you'll never have a problem of inheriting conflicting state from both interfaces, or inheriting two different final methods and not being able to resolve the conflict.

So, here are the answers to your questions:

  1. To be able to introduce new methods in existing interfaces without breaking backward compatibility: existing implementations will automatically implement these methods since their implementation is in the base interface.
  2. No, because that would introduce a diamond problem.
  3. Irrelevant
like image 193
JB Nizet Avatar answered Oct 06 '22 22:10

JB Nizet


Regarding Point 1:

In order to support lambda expression for all collection classes, like forEach method, it was necessary to add something which will have backward compatibility.

see this video for detail Lambda Peak Under the hood

like image 35
Vinit Prajapati Avatar answered Oct 06 '22 21:10

Vinit Prajapati