Skip to main content

Command Palette

Search for a command to run...

Default Method Java 8

Published
โ€ข1 min readโ€ขView as Markdown

๐ƒ๐ž๐Ÿ๐š๐ฎ๐ฅ๐ญ ๐ฆ๐ž๐ญ๐ก๐จ๐

One of the news of JAVA 8 , itโ€™s simply a keyword โ€œdefaultโ€ added to a method in an interface to make it as default .

๐‘พ๐’‰๐’š ๐’Š๐’•โ€™๐’” ๐’‚๐’…๐’…๐’†๐’… !?

Sometimes we have many classes implementing same interface A. After a time , we want to add a new method to this interface . The problem we can face is that we should implement this method in all classes implementing this interface , which is a little bit hard (imagine 100 classes ๐Ÿ˜…). Here default keyword comes , in a way that we can create a method with itโ€™s implementation inside an interface ( interfaces support just abstract methods except static and default methods ) .

๐‘พ๐’‰๐’‚๐’• ๐’†๐’๐’”๐’† !? ๐‘พ๐’† ๐’„๐’‚๐’ ๐’–๐’”๐’† ๐’”๐’•๐’‚๐’•๐’Š๐’„ ๐’Ž๐’†๐’•๐’‰๐’๐’…๐’” , ๐’Š๐’• ๐’…๐’๐’†๐’” ๐’•๐’‰๐’† ๐’”๐’‚๐’Ž๐’† ๐’•๐’‰๐’Š๐’๐’ˆ .

No,static methods cannot be overrided , otherwise we can override default methods .

Defaults methods could be called via the instance of the class implementing the interface, static methods could be called directly from the interface without making an instance .

Thanks for reading ๐Ÿ™๐Ÿป