Tuesday, May 7, 2019

Interface In Java 8

As before java 8, interface can have only abstract methods and we need to provide the implementation of these methods into implementation classes. Once we have design an interface and implements this into to our application than after any changes into interfaces are big task because if we add any new methods into interface than we require to add the implementation of that methods in all the implementation class where we had use this interface.
 As interface grows old, the number of classes implementing it might grow to an extent that it’s not possible to extend interfaces.
That’s why when designing an application, most of the frameworks provide a base implementation class and then we extend it and override methods that are applicable for our application.

Java 8 Interface Changes – static method, default method

Java Interface Default Method:

To creating a default method in java interface, we have to use “default” keyword with the method signature.

package com.shubh.defaultmethod.example;

public interface ExampleInterface {

 void method1(String str);

 default void print(String str) {
  System.out.println("I1 logging::" + str);
 }
}
Now in java 8, it is not mandatory to provide implementation for default methods of interface in impelemntation class.
This feature of java 8, help us in extending interfaces with additional methods without providing implementation.

Java Interface Static Method: 
  • Static method of java interface is like default method except that we can’t override static methods in the implementation classes.
  • Java interface static method is visible to interface methods only.
  • We cannot override static methods.
  • This feature helps us to avoiding poor implementation in implementation classes.
  • Static methods are used like utility methods. Example- To check null values, collection sorting etc.
  • Static method of Java 8 interface, helps us to providing security by not allowing implementation classes to override these static methods.
  •  Interface static method can’t define for Object class methods, compiler will throw the compiler error as “This static method cannot hide the instance method from Object”. Because this is not allowed in java, due to Object is the base class for all the classes and we can’t have one class level static method and another instance method with same signature.




Q- How do you define an interface in Java?
Q- What is functional interface in java?
Q- What is the use of default method in interface in Java 8?
Q- What is the use of static method in interface?
Q- Can Java interface have method body?
Q- Can we override default method in Java 8?
Q- Can interface have constructor in Java?
Q- Can interface methods be final?
Q- Why interface is used in Java? Or Why interface is important in Java?
Q- Can we have static methods in interface?
Q- Why do we need static method in interface? Or What is the purpose of static method in Java?
Q- Can we declare static variable in interface?
Q- Can we override static method?

No comments:

Post a Comment