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](https://java2blog.com/interface-in-java-with-example/ “interface”) and [abstract class](https://java2blog.com/abstract-class-java/ “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

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • Unable to obtain LocalDateTime from TemporalAccessor
    20 February

    [Fixed] Unable to obtain LocalDateTime from TemporalAccessor

    Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTimeWas this post helpful? In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You […]

  • Java LocalDate to Instant
    17 February

    Convert LocalDate to Instant in Java

    Table of ContentsJava LocalDate to InstantUsing toInstant() wth ZoneIdUsing toInstant() with ZoneOffsetWas this post helpful? In this article, we will see how to convert LocalDate to Instant in Java. Java LocalDate to Instant Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone. […]

  • Java Instant to LocalDate
    17 February

    Convert Instant to LocalDate in Java

    Table of ContentsUsing ofInstant method [ Java 9+]Using ZoneDateTime’s toLocalDate() [Java 8]Was this post helpful? In this article, we will see how to convert Instant to LocalDate in java. Using ofInstant method [ Java 9+] Java 9 has introduced static method ofInstant() method in LocalDate class. It takes Instant and ZoneId as input and returns […]

  • Convert String to LocalDateTime in Java
    17 February

    Convert String to LocalDateTime in Java

    Table of ContentsJava String to LocalDateTimeConvert String to LocalDateTime with custom formatWas this post helpful? In this article, we will see how to convert String to LocalDateTime in Java. LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default. […]

  • Java LocalDateTIme to String
    16 February

    Format LocalDateTime to String in Java

    Table of ContentsJava LocalDateTime To StringConvert LocalDateTime to Time Zone ISO8601 StringParse String to LocalDateTimeWas this post helpful? In this article, we will see how to format LocalDateTime to String in java. Java LocalDateTime To String To format LocalDateTime to String, we can create DateTimeFormatter and pass it to LocalDateTime’s format() method. [crayon-62b8ef4bad74e275319909/] Here are […]

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()ConclusionWas this post helpful? Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.