Interface default methods in java 8

Interface default methods in java
In this post, I will introduce default methods in interface which are introduced in  Java 8.
Have you ever faced a situation, when you created an interface and many classes implemented that interface. Now you need to add new methods to interface. After adding new methods, your java project will be full of compilation errors because you need to add these new methods to all classes which are implementing that interface (If a class implement an interface then you have to implement all its methods in the class)

Let’s take an example:
Create an interface called Decorable

Now Create a class called Room which will implement this interface.

Now this was your current implementation and now you have a new way of decorating and you want to add it to Decorable inteface.When you add this method to Decorable interface

In Room class, you will get following error:

What if We use default method:

As you can see, we have made decorateWithPaints() default and we will not get any error in Room class as we have provided default implementation in Decorable interface.

Why default methods

The oneliner for this could be “backward compatibility”.If JDK modifies an interface, then all classes which implements this interface will break.

For adding lambda expression in Java 8, JDK needs to add  methods(such as foreach) to List or collections Interface, but if you add this method to these interface, it will break millions lines of code as class which implements the interface, need to implement all its methods.

By adding default method in interface, you can provide default implementation of it without affecting implementing classes as it includes implementation of that method and any implementing class which needs that method can override it.

What about multiple Inheritance?

Adding default implementation to the interface can give rise to ambiguity in multiple inheritance. As two interface can provide same default method and there can be ambiguity while calling.Java 8 will give you compile time exception when this kind of situation will arise.

so it will give you compile time error as below:

you can solve this compilation error by overriding decorateWithPaints method in Room class

Difference between default methods and abstract class

Introduction of default methods to interface bridge gap between interface and abstract class.Now interface looks very similar to abstract classes but there are still differences. Lets list them

Parameter
Abstract class
Interface with default methods
State of objects
Abstract class can hold state of object
Interface with default methods can not hold state of objects
Access Modifier
Abstract class methods can have public ,protected,private and default modifier
Interface methods are by default public. you can not use any other access modifier with it
Constructor
Abstract class can have constructor
Interface  can not have constructor
Member variables
It can have member variables

import_contacts

You may also like:

Related Posts

  • Format Instant to String in java
    02 May

    How to format Instant to String in java

    Learn about how to format Instant to String in java.

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.Date In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    Table of ContentsUsing Instant objectUsing java.sql.Date In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which […]

  • Convert Stream to List in java
    31 December

    Java Stream to List

    Table of ContentsUsing Collectors.toList()Using Collectors.toCollection()Using foreachFilter Stream and convert to ListConvert infinite Stream to List In this post, we will see how to convert Stream to List in java. There are multiple ways to convert Stream to List in java. Using Collectors.toList() You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    Table of ContentsLocalDateTimeTimestampConvert LocalDateTime to Timestamp In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; […]

  • 28 August

    A In-Depth guide to Java 8 Stream API

    Table of ContentsIntroductionTypes of Stream operationsStream creationEmpty StreamCollection StreamStream.ofStream.generate()Stream.iterate()Lazy evaluationOrder of operationsPrimitive StreamsConvert Stream to IntStreamConvert IntStream to StreamEmployee classCommon intemediate operationsMap()Filter()sorted()limit()Skip()flatmap()Common terminal operationsforeachcollectReducecountallMatch()nonMatch()anyMatch()min()max()Parallel StreamsExercisesExercise 1Exercise 2Exercise 3Exercise 4Excercise 5Excercise 6Excercise 7 In this post, we will see an in-depth overview of Java 8 streams with a lot of examples and exercises. Introduction You may […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.