Interfaces in Java can now do much more than just define abstract methods. Since Java 8+, we can also use default, static, and even private methods โ and when interfaces extend each other, you may wonder:
Can this method override that one?
Letโs answer that with a definitive override matrix โ โ
๐งช Reference Interface
Weโll use this base interface to discuss method overrides:
Now, say we create a subinterface:
๐ Override Possibilities Matrix
๐ Hiding means the static method is not overridden but redefined in the subinterface or implementing class.
๐ก Examples
โ Valid
โ Invalid
๐ง Key Takeaways
- โ You can override abstract or default methods with abstract or default.
- โ You cannot override them with static.
- โ You can redeclare static or private methods as any type โ but they do not override, just hide or coexist.
- ๐ Private methods are invisible to the subinterface, so any redefinition is treated as a new method.
๐งพ Save This Matrix!
๐ Tags
#Java #JavaInterfaces #Java8 #Java21 #JavaCertification #OverrideVsHide #SoftwareEngineering #CleanCode #JavaTips #TechInterview
Go further with Java certification:
Java๐
Spring๐