Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I am confused about when to use a Java interface default method and an abstract method. Can somebody help me with this?

1 Answer

0 votes
by (13.1k points)

There is a lot more to abstract class than the default method implementations like private state. From Java 8, whenever you have the choice of going with the default method in the interface.

The constraint on the default method is that it can be only implemented only in terms of calls to other interface methods without having reference to a particular implementation’s state. So the main use case is higher-level and convenience methods.

Before you were forced to use an abstract class for the convenience methods, thus constraining the implementor to single inheritance, now you can have a really clean design with just the interface and a minimum effort on the programmer.

The default methods were introduced to Java 8 to extend the collections framework interfaces with lambda-oriented methods without breaking any existing implementations. Though this is more relevant to the authors of public libraries, you may find that the same feature is useful in your project as well. You have got one centralized place where to add new convenience and you do not have to rely on how the rest of the type hierarchy looks.

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Sep 26, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...