0
0
Javaprogramming~10 mins

Default methods in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default methods
Interface with default method
Class implements interface
Class inherits default method
Optional: Class overrides default method
Object calls method
If overridden, use class method
Else, use interface default method
This flow shows how a class implementing an interface can use or override a default method defined in that interface.
Execution Sample
Java
interface MyInterface {
    default void greet() {
        System.out.println("Hello from default method");
    }
}

class MyClass implements MyInterface {}

public class Main {
    public static void main(String[] args) {
        new MyClass().greet();
    }
}
This code shows a class using a default method from an interface without overriding it.
Execution Table
StepActionEvaluationResult
1Define interface MyInterface with default method greet()Method greet() available with default implementationInterface ready
2Define class MyClass implementing MyInterfaceNo override of greet() in MyClassMyClass inherits default greet()
3Create new MyClass objectObject createdInstance ready
4Call greet() on MyClass instanceNo override found in MyClassCalls default greet() from MyInterface
5Execute greet() method bodyPrint statement runsOutput: Hello from default method
6Program endsNo more instructionsExecution stops
💡 Program ends after calling default method greet() from interface
Variable Tracker
VariableStartAfter Step 3After Step 4Final
MyClass instancenullObject createdSame object calls greet()Object exists until program ends
Key Moments - 2 Insights
Why does MyClass have the greet() method even though it is not defined there?
Because MyClass implements MyInterface, it inherits the default greet() method defined in the interface, as shown in step 4 of the execution_table.
What happens if MyClass defines its own greet() method?
If MyClass overrides greet(), the class method is called instead of the interface default, replacing the behavior seen in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed when greet() is called on MyClass instance?
ANothing is printed
B"Hello from MyClass"
C"Hello from default method"
DCompilation error
💡 Hint
Refer to step 5 in execution_table where the default greet() prints the message
At which step does the program create the MyClass object?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check the action column in execution_table for object creation
If MyClass overrides greet(), which step changes in the execution_table?
AStep 4
BStep 5
CStep 3
DStep 1
💡 Hint
Step 4 shows method call resolution; overriding changes which method is called
Concept Snapshot
interface InterfaceName {
    default void methodName() {
        // default implementation
    }
}

class ClassName implements InterfaceName {
    // inherits default method or overrides it
}

- Default methods allow interfaces to provide method bodies.
- Classes implementing the interface inherit these methods.
- Classes can override default methods to change behavior.
Full Transcript
This example shows how Java interfaces can have default methods with implementations. A class implementing such an interface inherits the default method if it does not override it. When the method is called on an instance of the class, the default method runs, printing a message. If the class overrides the method, its version runs instead. This allows interfaces to add new methods without breaking existing classes.