Java-8 Interfaces
Published: 2022-01-02 02:32:29

                      Interfaces - from java 1.8 version

**From Java 1.8 version onwards, we can write default and static methods inside interfaces:**

**<u>Default Methods:</u>**

1. Default methods in interfaces are useful when you want to add new methods to the interface without affecting the implementation classes. Default methods are otherwise called as <u>_Defender Methods or Virtual Extension Methods_</u>.

2. Default methods must be implemented(we should implement the method while declaring inside interface) because Interface-default methods are by default available to implementation classes.

3. But, variables declared inside interfaces are by default "**public static final**"

**When to use the above default methods concept:**

1. Let's assume we created an interface with two (2) methods and more than hundred (100) classes are implementing the above interface. Now, there is a requirement to add one more method in the same interface. But, that method is used by only one(1) implementation class. If we add one more method(named thirdMethod) in the same interface for the sake of just one(1) implementation class, we need to provide the implementation of the newly added method(named thirdMethod) in all other 99 implementation classes, otherwise we need to declare all 99 classes as abstract till java 1.7.

2. "**From java 1.8, if we declare that newly added method(named thirdMethod) in the interface as "default" then there is no need of implementing the newly added method in all other (99) implementation classes and moreover declaring the classes as abstract is also not needed.**"

For sample program visit this URL: [Default Methods Demo Program](https://github.com/Yashwanth-G/java8-/blob/master/DefaultMethodsDemo)

**Note:** Object class methods are not allowed to use as default methods inside interface.

**<u>Static Methods:</u>**

1. "**Static methods**" are also allowed inside interfaces from java 1.8 we can declare main method and run interface directly. To use interface there is no need of separate implementation class from java 1.8 onwards. But, Interface "static methods" by default not available to implementation classes unlike default methods.

2. Interface static methods should be called using Interface name only.

**When to use the above static methods concept:** If everything is static the best approach is to go for interfaces than classes because when everything is static it is no where related to object. When it is not related to object it is not good practice to create class. Moreover, class is more costly than interfaces.


![Interface Declaration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxn20thsrab032cetjrz.jpg)

The above code works fine from java 1.8 onwards.

**Point to be noticed:** Let's assume we have an interface Animal with default method move(). We also have another interface Amphibian with same default method move(). Note that both the interfaces have same implementation of default method move(). Now, if a class named Frog implements both the above interfaces Animal and Amphibian we get an error saying "class Frog inherits unrelated defaults for move() from types Animal and Amphibian". 

Therefore, while using default methods in interfaces we are not allowed to have a class(Frog) that implements two interfaces(Animal and Amphibian) with the same default methods without providing a overridden implementation of default method move() in Frog class.

[For sample program click here](https://github.com/Yashwanth-G/java8-/blob/master/DefaultMethodsProblem)

Thank you for reading this article :)