- Java 8 introduced two new methods in interface they are
1.default methods
2.static methods - By this interfaces and abstract class are same but still having lot differences like abstract class can have a constructor etc will discuss more before that we need to know about these Java 8 features of interfaces.
- Defaults methods are also called as defender methods can be implemented inside the interface
- Like normal class now with java 8 we can declare static methods in side a interface.
- Lets jump deep into Java 8 default and static methods
Interface Default Methods in Java 8
- Before Java 8 in interfaces we can and able to declare only abstract methods only.
- If we declare a method without abstract that will be treated as abstract by default.
- As we know all methods in interfaces are by default abstract methods.
- These methods wont have body means implementations
- The class which is implementing this interface need to provide body / implementation for this abstract methods.
- Now with java 8 default methods we can add methods to interface without disturbing existing functionality.
- So instead of overriding now we can inherit these default methods from interfaces
- Defaults methods are also known as defender methods or virtual extension methods
- Default methods will help us to avoid utility classes.
- We can define utility methods inside the interface and use it in all classes which is implementing.
- One of the major reason to introduce this default methods in java 8 is to support lambda expressions in collections API and to enhance.
- package com.instanceofjava;
- interface Java8Interface{
- abstract void show();
- default void display(){
- System.out.println("default method of interface");
- }
- }
- package com.instanceofjava;
- class Sample implements Java8Interface {
- void show(){
- System.out.print("overridden method ")
- }
- public static void main(String[] args){
- Sample obj= new Sample();
- obj.show(); // calling implemented method
- obj.display(); // calling inherited method
- Java8Interface.display(); calling using interface name
- }
- }
Output:
- overridden method
- default method of interface
- default method of interface
How to call default methods:
- We can all these default methods by using interface name and also by using object of the class which is implementing.
- From above example
- obj.show(); // calling implemented method
- obj.display(); // calling inherited method
- Java8Interface.display(); calling using interface name
Can we override java 8 default method
- As we discussed above default methods in interfaces are implemented methods with bodies
- Yes we can override same method in class which is implementing this interface.
- Lets see one sample program how to override and what happens if we override
- package com.instanceofjava;
- interface InterfaceWithDefault{
- default void defMethod(){
- System.out.println("default method of interface");
- }
- }
- package com.instanceofjava;
- class Demo implements InterfaceWithDefault{
- void defMethod(){
- System.out.print("overridden method in class Demo ")
- }
- public static void main(String[] args){
- Demo obj= new Demo();
- obj.defMethod(); // calling overridden method
- Java8Interface.defMethod(); calling using interface name : interface defMethod will be called
- }
- }
Output:
- overridden method in class Demo
- default method of interface
What happens if we implement two interfaces having same default methods
- Now lets see if a class implementing two interfaces which are having same default methods
- Whatever the implementation in the two interfaces defined if we implementing two interfaces which are having a default method in both then compilation error will come if two methods have same signature. works fine if two methods have same name with different arguments.
- Check the below example programs to understand more.
- package com.instanceofjava;
- interface A{
- default void defMethod(){
- System.out.println("default method of interface: A");
- }
- }
- package com.instanceofjava;
- interface B{
- default void defMethod(){
- System.out.println("default method of interface: B");
- }
- }
- package com.instanceofjava;
- class Demo implements A, B{ // compilation error will come
- public static void main(String[] args){
- Demo obj= new Demo();
- }
- }
- If we implement two interfaces which are having same method with same parameters then compilation error will occur.
- Duplicate default methods named "defMethod" with the parameters () and () are inherited from the types A and B.
- If we define two methods with different type of parameters then we can work with both interfaces.
- package com.instanceofjava;
- interface A{
- default void defMethod(){
- System.out.println("Default method of interface: A");
- }
- }
- package com.instanceofjava;
- interface B{
- default void defMethod(String str){
- System.out.println("Default method of interface: B");
- System.out.println(str);
- }
- }
- package com.instanceofjava;
- class Demo implements A, B{ // compilation error will come
- public static void main(String[] args){
- Demo obj= new Demo();
- obj.defMethod();
- obj.defMethod("Java 8")
- }
- }
Output:
- Default method of interface: A
- Default method of interface: B
- Java 8
Interface Static Methods in Java 8
- Another Java 8 interface method is static method.
- Now we can define static methods inside interface but we can not override these static methods.
- These static method will act as helper methods.
- These methods are the parts of interface not belongs to implementation class objects.
- package com.instanceofjava;
- interface StaticInterface{
- Static void print(String str){
- System.out.println("Static method of interface:"+str);
- }
- }
- package com.instanceofjava;
- class Demo implements StaticInterface{
- public static void main(String[] args){
- StaticInterface.print("Java 8")
- }
- }
Output:
- Static method of interface: Java 8