Meaning of default method
A method in an interface having a body or implementation is called a default method.
The concept of default methods was introduced in java 8.
Till java 7, java interfaces could only have abstract methods which did not have any body but only their declaration.
But starting java 8, interfaces can also have methods with a body or an implementation. Such methods are called default methods.
Default interface methods are preceded with
default
keyword and should have a body or implementation and since they should have a default implementation, they are named as default methods, also known as defender methods.Only interfaces can have default methods. A class is not allowed to contain default methods.
default method example
Below is an example of an interface containing default methods.
public interface IPhone { // abstract method public String getColor(); // default method public default String printBrand() { System.out.print("Apple"); } }
Above interface contains one method without a body and a method which has a body(default method).
Note that this method is preceded with default
keyword.
Thus, if a class implements an interface having default methods, then these methods will be available to the objects of that class.
Example,
public class IPhoneX implements IPhone { // class method public String getColor() { return "Rose gold"; } }
Let us write a main class to test this.
public class DefaultDemo { public static void main(String[] args) { IPhoneX iphone = new IPhoneX(); // invoke default method iphone.printBrand(); // prints Apple } }
Note that we did not define the default method in the implementing class but it was still invoked by the object of this class.
abstract
keyword unlike interface methods till java 7.Why default methods?
Importance of default methods can be easily understood by going through below scenarios.
- If there is an interface that is implemented by a dozen classes.
Now if you add a new method declaration to this interface, then it will break all the classes since a class should implement all the methods defined in an interface.
Starting java 8, you can add a default method in an interface and it will not break any of the implementation classes. - Suppose there is an interface and it is implemented by many classes. Now you want to add a new method with the same implementation to all these classes.
Prior to Java 8, you need to add this method definition to all the classes because you could not add a new method definition(with body) to an interface.
With default methods in java 8 interfaces, you can add a method definition to an interface and it will be automatically available to all implementing classes.
Yes.
Because default methods of an interface contain a body and the same method is available to all the implementing class.
But it is not necessary that all classes require the same functionality for those methods.
In such cases, an implementing class can change the behavior of a default method by overriding it.
Example,
Below class overrides default method of the interface that it implements.
public class IPhoneX implements IPhone { // class method public String getColor() { return "Rose gold"; } // override interface default method @Override public String printBrand() { System.out.print("Apple Computers"); } }
Let us write a main class to test this.
public class DefaultDemo { public static void main(String[] args) { IPhoneX iphone = new IPhoneX(); // invoke default method iphone.printBrand(); // prints Apple Computers } }
When a class overrides the interface default method, it should follow all the method overriding rules in java. But below pointers should also be kept in mind
- Interface methods are implicitly
public
even if you do not writepublic
before them. Same applies to default methods also. - When overriding a default method in a class, you will have to add
public
specifier to it else there will be a compiler error. - You may or may not apply @Override annotation to the overridden method.
default
keyword should not be applied to overridden method.
Invoking interface default method from class
If you want to explicitly invoke the default method of an interface from a normal method or an overridden version of default method from its implementing class, then it can be done using syntax
Interface name.super.methodName
Example,
public class IPhoneX implements IPhone { // class method public String getColor() { // call interface default method IPhone.super.printBrand(); return "Rose gold"; } // override interface default method @Override public String printBrand() { // call interface default method IPhone.super.printBrand(); } }
Default methods and multiple inheritance
A class in java can implement multiple interfaces. But, if two interfaces contain a default method with the same name and signature, then the class cannot implement both these interfaces.
The reason for this restriction is shown in the below example.
// First interface public interface Fruit { // default method public default String getName() { return "Orange"; } } // Second interface public interface Nut { // default method public default String getName() { return "Almond"; } }
Above code contains two interfaces each having a default method with the same name.
A class that implements both these interfaces is written below.
The implementing class has a method which creates an object of this class and tries to call default method of interface.
Which method do you think should be called as both the methods(with same name) are available to the object?
public class FruitAndNut implements Fruit, Nut { pubilc void printName() { // create object of this class FruitAndNut candy = new FruitAndNut(); // Which method to call ?? candy.getName(); } }
In such a case, the compiler will stop you at the class definition by throwing below error since it is not able to figure out the exact method to call.
Duplicate default methods named getName with the parameters () and () are inherited from the types Fruit and Nut
Solution to this problem is that you need to specify the name of interface whose method you want to call as shown ahead.
public class FruitAndNut implements Fruit, Nut { pubilc void printName() { // call method of Fruit interface Fruit.super.getName(); } }
Default methods in java api
Some examples of default methods in java api are as follows
forEach
andsplitIterator
methods injava.lang.Iterable
interface.toArray
,removeIf
,splitIterator
,stream
andparallelStream
methods injava.util.Collection
interface.getOrDefault
,forEach
,replaceAll
,putIfAbsent
,remove
,replace
methods injava.util.Map
interface.replaceAll
,sort
andsplitIterator
methods injava.util.List
interface and many more.
There is no limit as to the number of default methods that can be written in an interface. You can have any number of default methods in an interface.
That is all on default methods in interface, introduced in java 8. Click the clap button below if the post was helpful.
Nice