Java 8 interface Changes | New features in java 8 Interface | Static, Default Methods
Java 8 interfaces can now have method declarations also for default and static methods. Upto java 7 we can have only method declarations in java interfaces, but now in java 8 we can declare default and static methods as well in java interfaces.
In java writing interfaces has always been a challange because with time the number of classes implementing an interface increases and adding any new method in the interface causes a change to all the implementing classes. But now from java 8 we can have some default methods in java interfaces which we will not have to implement in the implementing classes.
Java 8 interface - Creating default method -
To create a default method we provide the keyword 'default' with method signature and obviously provides the body of the method. For example -
package com.tutorialsinn.java8.interfaces; public interface InterfaceOne { // method without body void methodOne(); //default method with body default void printColor(String color){ System.out.println("car color is " + color); } }In above interface 'printColor(String color)' is a default method in interface 'InterfaceOne'. Now if any class implements this interface, it's not mandatory for that class to implement tthis interface.
What if some class implements two Interfaces with same default method ?
Suppose we have another interface InterfaceTwo with same default method -package com.tutorialsinn.java8.interfaces; public interface InterfaceTwo { //default method with body default void printColor(String color){ System.out.println("cat color is " + color); } }
We know that java doesn't allow a class to extent more than one class because it would arise a diamond problem - means if
both the superclasses had same method then compiler won't be able to decide which superclass method to use for the subclass.
But we know that java allow us to implement any number of interfaces, in that case this diamond problem colud arise again
if the class implements both the interfaces having same default method. Well in that case, we will get a compile time error,
and it becomes mandatory for the class to override the default method.
So to avoid this kind of problem we will have to override the default method inside the class implementing the
interfaces.
package com.tutorialsinn.java8.interfaces; public class Car implements InterfaceOne, InterfaceTwo{ String color; String name; int speed; public Car(String color, String name, int speed) { super(); this.color = color; this.name = name; this.speed = speed; } @Override public void methodOne() { } @Override public void printColor(String color) { System.out.println("printed from inside the car class"); System.out.println("car color is " + color); } }Notice that above class 'Car' has implemented both interfaces and hence overrides the common default method printColor(String color).
Now if we call the default method it will execute the method we overrided in the 'Car' class, see code below -
package com.tutorialsinn.java8.interfaces; public class RunCar { public static void main(String[] args) { Car car = new Car("White", "Ferrari", 250); car.printColor(car.color); } }The output of the above program will be -
printed from inside the car class car color is White
Java 8 Interface - static methods
We can use static methods also in java 8 interfaces, it's similar to default method except that any implementing class cannot override static methods. Static methods in Interfaces help us avoiding poor implementation of the static methods.To declare static method we use static keyword and provide method body. Interface static methods are not visible to the implementing class, so if we declare a method same as in interface in the implementing class then it's not method overriding. That's why if we try to put @override annotation on tha method in the implementing class we will get a compile time error. Static methods of java interfaces are only visible to the others methods of the same interface. Static methods can be directly called using the interface.Let's see a simple example of java 8 static methods in Interface-
let's create a simple static method 'isRunning' inside InterfaceOne, which is used another method of the same interface 'printSpeed';
package com.tutorialsinn.java8.interfaces; public interface InterfaceOne { void methodOne(); default void printColor(String color){ System.out.println("cat color is " + color); } static boolean isRunning(int speed){ return speed == 0 ? false : true; } default void printSpeed(int speed){ if(isRunning(speed)) System.out.println("car speed is "+speed); else System.out.println("Car is not running"); } }Now let's try to use the above Interface's static method -
package com.tutorialsinn.java8.interfaces; public class RunCar { public static void main(String[] args) { Car car = new Car("White", "Ferrari", 250); car.printSpeed(car.speed); } }Output - car speed is 250
OR we can use this static method directly by Interface as utility method -
package com.tutorialsinn.java8.interfaces; public class RunCar { public static void main(String[] args) { Car car = new Car("White", "Ferrari", 250); if(InterfaceOne.isRunning(car.speed)){ System.out.println("car is running"); } } }Output - car is running
So, these were some points about java 8 interfaces. Hope you understood it and will use the same for your future coding. In addition of this functional interfaces are also added in java 8 which we will discuss in our next articles about java 8.
No comments :
Post a Comment