Select Page

Java – What are Default methods in interface?


A default method is static and not abstract. It’s static means it belongs to the interface itself.


nanadwumor

May 25, 2023

Default methods in Interface


  • In Java 8, default methods were introduced.
  • default method is non-abstract and static method of an interface
  • when a class implements an interface, it overrides its methods and provides implementation for it

RECOMMENDED ARTICLES


Variables and Literals in Java
Variables and Literals in Java

Variables are used in many situations. For Instance, game applications use variables to collect your name when you sign in; to store and keep track of your number of trials, successes and failures. In Java, a variable is a container used to store...

double data type in Java
double data type in Java

The double data type is also a floating point number. It is called double because it has a double-precision decimal number.   The double data type is a floating point number. It is double precision data type The range is 4.9406564584124654 x...

The print and println methods in Java
The print and println methods in Java

The print() and println() methods are Java's predefined methods used to display text output on the console. These methods are part of the Java API. The print and println methods are used to print string of characters  onto the console The print...


A default method is a non-abstract, static method of an interface with implementation. That’s a default method is not abstract.

It belongs to the interface itself, not an instance of it (an interface cannot be instantiated) and has a body of code that can be executed by any implementing class.

Note that, when a class implements an interface, it overrides its methods and provides implementation for it. However, because a default method has implementation already, implementing classes normally don’t override them although they can.


Join Other Subscribers on Our YouTube Channel and Don’t Miss a thing!


Methods of interface were formerly all abstract methods. However, in Java 8, default methods were introduced.

A default method is static and not abstract. It’s static means it belongs to the interface itself.

Why was default method introduced in Java 8?

If an interface is implemented by a class, the class must override all of the interface’s methods and provides implementation for it.

Suppose you create an interface which is implemented by a class today. And in the future, you decide to add new features to the interface. The moment you add any new method to the interface, your classes that implement this interface must provide methods to override them.

Assume this interface is implemented by thousands of classes. Then you’d have to provide implementation for the new interface method in all the thousands of classes. That would be a nightmare. Isn’t it?

And if you fail to implement this new method in all the classes, the code will break down. Introducing default methods prevented this code breakdown because there will be no need to provide implement for a default method.

Instead of adding another abstract method that would require implementation from all the classes that implemented the interface, it will be efficient to add a default method because that won’t call for implementation from the thousand classes that implement the interface.

Program demonstrates default method

The interface is called Phone. It has two methods which are call() and message(). These methods are all abstract without any implementation.

Let’s look at two classes that implement the Phone interface. These are AndroidPhone and iPhone.

AndroidPhone class

Iphone class

Now, AndroidPhone class and iPhone class will implement the Phone interface.

Now, suppose years later, I realize that my classes – AndroidPhone and iPhone and any other class that provides similar function must be able to play computer game. Let’s call the method that ensures this function, playGame().

To ensure that all classes obey this new function, I have to add the new method to my Phone interface because this will force all classes to implement this new function.

There’s only one problem. The moment I add this new method to my interface, it’ll break my code written in the past. There will be an error unless I manually go through all the many classes that implement the Phone interface and implement this playGame() method in it. Imagine if I had about 1,000+ classes that implement the Phone interface. Horrific, isn’t it?

To ensure that I get this playGame() method or function in my code without breaking it or without having to go into each of the class one by one to implement it, I add a default method to the interface instead of an abstract method.

So, you see – a default method was introduced in Java 8 to solve the case of backward compatibility. Now let’s look at how this can be done.

Program demonstrates default method introduced in an interface to achieve a new functionality without breaking the code

Now, let’s look at the full program.

AndroidPhone and iPhone class implement the Phone interface

Output

default methods in Java

You May Also Like…


double data type in Java
double data type in Java

The double data type is also a floating point number. It is called double because it has a...



0 Comments

Submit a Comment

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