Java
Default Method
Programming
Java Coding
Method Calling

Explicitly calling a default method in Java

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Java, default methods were introduced as a part of Java 8, allowing developers to add new methods to interfaces without breaking the existing implementations of these interfaces. Default methods can be executed directly through interface instances providing flexibility, and also help in avoiding redundancy by sharing common method implementations.

Understanding Default Methods

Default methods are methods defined in an interface with the default modifier, and they provide an implementation. This capability was primarily added to support lambda expressions in Java. Prior to Java 8, interfaces could only have method signatures without implementations, except static methods introduced in Java 8 as well. However, with default methods, interfaces can have methods with a full body, helping in evolving APIs without breaking existing code.

Why Explicitly Call Default Methods?

Explicitly calling a default method usually comes into play in scenarios involving multiple inheritance of behavior. Java does not support multiple inheritance of state (as in inheriting from multiple classes), but it does support multiple inheritance of behavior (from multiple interfaces). When a class implements multiple interfaces that define default methods with the same signature, the compiler does not know which one to use, leading to the "diamond problem." To resolve such ambiguities, or to specifically leverage a default method's implementation from one interface over another, explicit calls to these methods become necessary.

How to Explicitly Call a Default Method

To illustrate, let’s consider you have two interfaces, Vehicle and Alarm, both providing a default method named turnOn:

java
1interface Vehicle {
2    default void turnOn() {
3        System.out.println("Vehicle is starting");
4    }
5}
6
7interface Alarm {
8    default void turnOn() {
9        System.out.println("Alarm is turning on");
10    }
11}

If a class, say Car, implements both these interfaces, ambiguity arises. To resolve this and to explicitly call the desired default method, you can use the following syntax in your implementing class:

java
1public class Car implements Vehicle, Alarm {
2
3    // Resolve ambiguity by choosing Vehicle's turnOn method
4    public void turnOn() {
5        Vehicle.super.turnOn();
6    }
7
8    public static void main(String[] args) {
9        new Car().turnOn(); // Prints: Vehicle is starting
10    }
11}

In this example Vehicle.super.turnOn(); is an explicit call to the turnOn method defined in the Vehicle interface.

Table: Key Points on Default Methods and Explicit Calls

FeatureDescriptionExample
Default MethodAdds implementation in interfacesdefault void turnOn() {..} in Vehicle
Use CaseAvoiding code duplication, evolving interfacesLambda expressions in Java 8
Explicit CallChoose specific interface method in ambiguityVehicle.super.turnOn(); in Car class

Additional Considerations

Impact on Design: Default methods can change the way interfaces and inheritance are used in Java. They enable behaviors to be shared and reused effectively but can complicate designs, especially when multiple interfaces are involved.

Best Practices: It’s judicious to use default methods when there is a need to add methods to interfaces for API evolution without breaking existing implementors. However, overuse might lead to cluttered designs and maintenance challenges.

Performance Implications: While default methods provide a significant advantage in terms of code reusability and maintenance, they should be used sensibly, keeping in mind the runtime performance and the increased complexity in understanding code flows due to added behaviors in interfaces.

Explicitly calling default methods in Java solves specific problems related to multiple inheritance and method conflicts, providing a structured way to handle potential ambiguities. This feature not only adds flexibility to interface design but also aids in creating more robust and adaptable codebases.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.