Lucas Freire
By Lucas Freire

Changes behavior interface - Java 8

Changes behavior interface - Java 8

What is interface in Java

Another way to achieve abstraction In Java. An interface is like a “abstract class” that used is used to group related methods. However, an interface have some features that distinct an abstract class:

  • An concrete class can implement one or more interfaces

  • An interface can extend one or more interfaces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


public class Test implements Interface1,Interface2,Interface3 {

    //methods...

}



public interface Interface extends Comparator<T>, Runnable {



}





If you try extends interface in concrete class, you will have a java compiler error. The same will pass, whether try implements interface using another interface.

These examples will be compiler error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


public interface Test implements Interface1,Interface2,Interface3 {

    //methods...

}



public class Interface extends Comparator<T>, Runnable {



}





What change

With Java 8, the interface can have concrete methods and static methods. Before this version, the interface could not have concrete methods or static methods.

For create a concrate method in interface, the method must be the keyword default in the signature. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


public default void test(){

 //code

}



public static void staticMethod(){

 //code

}



With this, it is possible add to interface some methods without broken classes that implement it(maintaining compatibility).

For example, before java 8 the interface List does not have the method “sort”.

If the java programmers wanted to add behavior in the java.util.List without change your subtypes (Vector, ArrayList and others), will be not possible. If they add the behavior, your subtypes will be obligated to change your code to compile and maintain compability.

To make the behavior transparent to your subtypes, now they can use the default method to add some features without obligated your subtypes change code.

Inherits Behavior

Imagine you have two interfaces with the same default method. What happens with subtype implements the two interfaces?

The subtype will be obligated override the method. If no, will have a compiler error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public interface Test1 {



	public default void test() {

		System.out.println("Test 1 method");

	}



}



public interface Test2 {



	public default void test() {

		System.out.println("Test 2 method");

	}



}



public class Test implements Test1, Test2 {

// Java compiler error



}



If the interface have static methods, you must use the interfacename.staticmethodname.

When we extend the class with static method, you can use the subclass to call the static method. However, if the interface it is not possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


public class Parentclass {



	public static void testeMethodParent() {



	}



}



public interface Test1 {



	public default void test() {

		System.out.println("Test 1 method");

	}



	public static void teste() {



	}



}



public class Test extends Parentclass implements Test1 {



	public static void main(String[] args) {

		Test.testeMethodParent(); // OK

		Test1.testeStaticMethodInterface(); // OK

		Test.testeStaticMethodInterface(); // ERROR

	}



}