Explain how Java classes inherit and invoke interface default methods, then override or explicitly delegate to them. Work through dynamic dispatch and conflicting defaults without confusing instance methods with static interface methods.
What is a default method in a Java interface? Explain how an implementing class uses it, how that class can override it, and how it can explicitly invoke the interface's default implementation.
### Constraints & Assumptions
- Distinguish a class implementing an interface from a class extending another class.
- Use a small illustrative interface rather than assuming an existing application API.
- Explain what happens if two unrelated interfaces provide conflicting defaults.
### Clarifying Questions to Ask
- Does the implementing class inherit the default unchanged or override it?
- Is the interface a direct superinterface of the class making the explicit call?
- Does the class also inherit a concrete method with the same signature from a superclass?
```hint
An ordinary call through an interface reference still uses the object's applicable implementation. A qualified `super` call serves a different purpose.
```
### What a Strong Answer Covers
Describe a default instance method with a body, show normal invocation on an implementing object, and explain overriding and qualified invocation. Include the distinction from static interface methods and the rules that prevent ambiguous inheritance of unrelated defaults.
### Follow-up Questions
- How would a class choose between conflicting defaults from two unrelated interfaces?
- Which takes precedence: a suitable inherited class implementation or an interface default?
- Can an external caller use `InterfaceName.super.method()` to bypass an object's override?
Overview: Explain how Java classes inherit and invoke interface default methods, then override or explicitly delegate to them. Work through dynamic dispatch and conflicting defaults without confusing instance methods with static interface methods.
What is a default method in a Java interface? Explain how an implementing class uses it, how that class can override it, and how it can explicitly invoke the interface's default implementation.
Constraints & Assumptions
Distinguish a class implementing an interface from a class extending another class.
Use a small illustrative interface rather than assuming an existing application API.
Explain what happens if two unrelated interfaces provide conflicting defaults.
Clarifying Questions to Ask Guidance
Does the implementing class inherit the default unchanged or override it?
Is the interface a direct superinterface of the class making the explicit call?
Does the class also inherit a concrete method with the same signature from a superclass?
What a Strong Answer Covers Guidance
Describe a default instance method with a body, show normal invocation on an implementing object, and explain overriding and qualified invocation. Include the distinction from static interface methods and the rules that prevent ambiguous inheritance of unrelated defaults.
Follow-up Questions Guidance
How would a class choose between conflicting defaults from two unrelated interfaces?
Which takes precedence: a suitable inherited class implementation or an interface default?
Can an external caller use
InterfaceName.super.method()
to bypass an object's override?