|
1 | 1 | :java-package: src/org/j6toj8/languageenhancements |
2 | 2 | :section-java-package: ../../../{java-package} |
3 | 3 |
|
4 | | -=== Métodos `static` e `default` em Interfaces |
| 4 | +=== Static and default methods of an interface |
5 | 5 |
|
6 | | -.Objetivo |
| 6 | +.Objective |
7 | 7 | ---- |
8 | 8 | Use static and default methods of an interface including inheritance rules for a default method. |
9 | | -- |
10 | | -Usar métodos static e default de uma interface, incluindo regras de herança para um método default. |
11 | 9 | ---- |
12 | 10 |
|
13 | | -É esperado que o candidato saiba compreender e analisar o uso da dos modificadores `static` e `default` em métodos de interfaces. |
| 11 | +It is expected that the candidate can understand and analyze the use of `static` and `default` modifiers in interface methods. |
14 | 12 |
|
15 | | -Antes de continuar, com base no exemplo a seguir, entenda a execução do método `main` e o que é apresentado no console após sua execução. |
| 13 | +Before proceeding, based on the following example, understand the execution of the `main` method and what is presented on the console after its execution. |
16 | 14 |
|
17 | 15 | [source,java,indent=0] |
18 | 16 | .{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Complete.java |
19 | 17 | ---- |
20 | 18 | include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Complete.java[tag=code] |
21 | 19 | ---- |
22 | 20 |
|
23 | | -.Saída no console |
| 21 | +.console output |
24 | 22 | [source,console] |
25 | 23 | ---- |
26 | 24 | 10.0 |
27 | | -Correndo |
28 | | -Pessoa Correndo Rápido |
| 25 | +Running |
| 26 | +Fast Running Person |
29 | 27 | ---- |
30 | 28 |
|
31 | | -O código anterior possui dois modificadores novos para interfaces, possíveis desde o Java 8: `default` e `static`. É possível perceber que esses dois métodos possuem corpo, algo que não era possível antes em uma interface. Então, vamos entender quais são as novas possibilidades. |
| 29 | +The previous code has two new interface modifiers, possible since Java 8: `default` and `static`. You can see that these two methods have a body, something that was not possible before in an interface. So let's understand what the new possibilities are. |
32 | 30 |
|
33 | | -. Desde o Java 8, Interfaces podem ter métodos com o modificador `static`. |
| 31 | +. Since Java 8, Interfaces can have methods with the `static` modifier. |
34 | 32 |
|
35 | | -. Métodos com o modificador `static` em interfaces são chamados iguais aos de uma classe comum, ou seja, não fazem parte da API da interface. Dessa forma, não são herdados pelas classes que implementam essa interface. |
| 33 | +. Methods with the `static` modifier on interfaces are called the same as those of a standard class, i.e., they are not part of the interface API. So, they are not inherited by classes that implement this interface. |
36 | 34 | + |
37 | 35 | [source,java,indent=0] |
38 | 36 | .{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Static.java |
39 | 37 | ---- |
40 | 38 | include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Static.java[tag=code] |
41 | 39 | ---- |
42 | 40 |
|
43 | | -. Desde o Java 8, Interfaces podem ter métodos com o modificador `default`. |
44 | | - |
45 | | -. Métodos `default` não precisam, mas podem, ser sobrescritos. |
| 41 | +. Since Java 8, Interfaces can have methods with the `default` modifier. |
46 | 42 |
|
| 43 | +. `Default` methods do not need, but can be overridden. |
47 | 44 | + |
48 | 45 | [source,java,indent=0] |
49 | 46 | .{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Default.java |
50 | 47 | ---- |
51 | 48 | include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Default.java[tag=code] |
52 | 49 | ---- |
53 | 50 | + |
54 | | -Veja que a classe `Pessoa` não sobrescreve o método `correr()`, mantendo o comportamento padrão da implementação feita na interface `Corredor`. |
| 51 | +Note that the `Person` class does not override the `run()` method, maintaining the default implementation behavior of the `Runner` interface. |
55 | 52 | + |
56 | | -A classe `Cavalo`, por outro lado, sobrescreve o método `correr()` para ter sua própria implementação. |
| 53 | +The `Horse` class, on the other hand, overrides the `run()` method to have its own implementation. |
57 | 54 | + |
58 | | -.Saída no console |
| 55 | +.console output |
59 | 56 | [source,console] |
60 | 57 | ---- |
61 | | -Correndo |
62 | | -Galopando |
| 58 | +Running |
| 59 | +Galloping |
63 | 60 | ---- |
64 | 61 |
|
65 | | -. Assim como os outros método de uma interface, os métodos `static` e `default` *são sempre `public`*, e não podem ser modificados para `private` ou `protected`. |
| 62 | +. Like the other methods of an interface, the `static` and `default` *methods are always `public`*, and cannot be changed to `private` or `protected`. |
66 | 63 | + |
67 | 64 | [source,java,indent=0] |
68 | 65 | .{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_AccessModifiers.java |
69 | 66 | ---- |
70 | 67 | include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_AccessModifiers.java[tag=code] |
71 | 68 | ---- |
72 | 69 |
|
73 | | -. Diferente dos outros método de uma interface, os métodos `static` e `default` não são `abstract`, e também não podem ser. Afinal, eles possuem implementação. Apenas métodos sem implementação são `abstract`. |
74 | | - |
| 70 | +. Unlike other interface methods, the `static` and `default` methods are not `abstract`, nor can they be. After all, they have implementation. Only methods without implementation are `abstract`. |
75 | 71 | + |
76 | 72 | [source,java,indent=0] |
77 | 73 | .{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Abstract.java |
78 | 74 | ---- |
79 | 75 | include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Abstract.java[tag=code] |
80 | 76 | ---- |
81 | 77 |
|
82 | | -. Se uma classe implementa duas interfaces que possuem métodos `default` repetidos, ela obrigatoriamente deve implementar o seu próprio. |
| 78 | +. If a class implements two interfaces that have repeated `default` methods, it must implement its own. |
83 | 79 | + |
84 | 80 | [source,java,indent=0] |
85 | 81 | .{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefault.java |
86 | 82 | ---- |
87 | 83 | include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefault.java[tag=code] |
88 | 84 | ---- |
89 | 85 |
|
90 | | -. Ao implementar múltiplas interfaces, é possível acessar a implementação `default` de uma delas. |
| 86 | +. By implementing multiple interfaces, you can access the `default` implementation of one of them. |
91 | 87 | + |
92 | 88 | [source,java,indent=0] |
93 | 89 | .{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefaultSuper.java |
94 | 90 | ---- |
95 | 91 | include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefaultSuper.java[tag=code] |
96 | 92 | ---- |
97 | 93 | + |
98 | | -.Saída no console |
| 94 | +.console output |
99 | 95 | [source,console] |
100 | 96 | ---- |
101 | | -Correndo |
| 97 | +Running |
102 | 98 | ---- |
103 | 99 |
|
104 | | -. Quando uma interface herda de outra interface métodos `default`, estes podem ser mantidos, transformados em `abstract` ou redefinidos. |
| 100 | +. When an interface inherits from another interface, `default` methods can be retained, transformed into `abstract` or redefined. |
105 | 101 | + |
106 | 102 | [source,java,indent=0] |
107 | 103 | .{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_InterfaceInheritance.java |
108 | 104 | ---- |
109 | 105 | include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_InterfaceInheritance.java[tag=code] |
110 | 106 | ---- |
111 | 107 | + |
112 | | -Nesse exemplo, a interface `Piloto` herda de `Corredor` e mostra 3 cenários distintos: |
| 108 | +In this example, the `Pilot` interface inherits from `Runner` and shows 3 distinct scenarios: |
113 | 109 |
|
114 | | -* Mantém o método `correr()` inalterado; |
115 | | -* Altera o método `correrRapido()` para que seja `abstract`, fazendo com que qualquer classe que implemente a interface `Piloto` tenha que implementar esse método; |
116 | | -* Altera o método `correrDevagar()` para que tenha sua própria implementação |
| 110 | +* Keep `run()` method unchanged; |
| 111 | +* Changes the `runFast()` method to be `abstract`, so any class that implements the `Pilot` interface has to implement this method; |
| 112 | +* Change `runSlow()` method to have its own implementation |
117 | 113 |
|
118 | | -.Referências |
| 114 | +.References |
119 | 115 | **** |
120 | 116 |
|
121 | 117 | * Designing an Interface |
122 | 118 | + |
123 | | -Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 48). Wiley. Edição do Kindle. |
| 119 | +Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 48). Wiley. Kindle Edition. |
124 | 120 |
|
125 | 121 | * https://www.baeldung.com/java-static-default-methods[Static and Default Methods in Interfaces in Java.] |
126 | 122 |
|
|
0 commit comments