Skip to content

Commit 6159564

Browse files
committed
feat(lang-enh): 🎸 static/default methods interface, translated
Refers: #4
1 parent 81639b9 commit 6159564

9 files changed

+106
-110
lines changed

book/02-language-enhancement/sections/04-static-default-in-interfaces.asc

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,122 @@
11
:java-package: src/org/j6toj8/languageenhancements
22
:section-java-package: ../../../{java-package}
33

4-
=== Métodos `static` e `default` em Interfaces
4+
=== Static and default methods of an interface
55

6-
.Objetivo
6+
.Objective
77
----
88
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.
119
----
1210

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.
1412

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.
1614

1715
[source,java,indent=0]
1816
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Complete.java
1917
----
2018
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Complete.java[tag=code]
2119
----
2220

23-
.Saída no console
21+
.console output
2422
[source,console]
2523
----
2624
10.0
27-
Correndo
28-
Pessoa Correndo Rápido
25+
Running
26+
Fast Running Person
2927
----
3028

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.
3230

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.
3432

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.
3634
+
3735
[source,java,indent=0]
3836
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Static.java
3937
----
4038
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Static.java[tag=code]
4139
----
4240

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.
4642

43+
. `Default` methods do not need, but can be overridden.
4744
+
4845
[source,java,indent=0]
4946
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Default.java
5047
----
5148
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Default.java[tag=code]
5249
----
5350
+
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.
5552
+
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.
5754
+
58-
.Saída no console
55+
.console output
5956
[source,console]
6057
----
61-
Correndo
62-
Galopando
58+
Running
59+
Galloping
6360
----
6461

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`.
6663
+
6764
[source,java,indent=0]
6865
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_AccessModifiers.java
6966
----
7067
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_AccessModifiers.java[tag=code]
7168
----
7269

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`.
7571
+
7672
[source,java,indent=0]
7773
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Abstract.java
7874
----
7975
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Abstract.java[tag=code]
8076
----
8177

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.
8379
+
8480
[source,java,indent=0]
8581
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefault.java
8682
----
8783
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefault.java[tag=code]
8884
----
8985

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.
9187
+
9288
[source,java,indent=0]
9389
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefaultSuper.java
9490
----
9591
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefaultSuper.java[tag=code]
9692
----
9793
+
98-
.Saída no console
94+
.console output
9995
[source,console]
10096
----
101-
Correndo
97+
Running
10298
----
10399

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.
105101
+
106102
[source,java,indent=0]
107103
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_InterfaceInheritance.java
108104
----
109105
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_InterfaceInheritance.java[tag=code]
110106
----
111107
+
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:
113109

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
117113

118-
.Referências
114+
.References
119115
****
120116
121117
* Designing an Interface
122118
+
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.
124120
125121
* https://www.baeldung.com/java-static-default-methods[Static and Default Methods in Interfaces in Java.]
126122

src/org/j6toj8/languageenhancements/staticdefaultininterfaces/StaticDefaultInInterfaces_Abstract.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
public class StaticDefaultInInterfaces_Abstract {
66

77
// tag::code[]
8-
interface Corredor {
9-
default String correr() { // COMPILA - método default não é abstract
10-
return "Correndo";
8+
interface Runner {
9+
default String run() { // COMPILES - default method is not abstract
10+
return "Running";
1111
}
1212

13-
abstract default String correrRapido() { // NÃO COMPILA - método default não pode ser declarado abstract
14-
return "Correndo Rápido";
13+
abstract default String runFast() { // NOT COMPILING - default method cannot be declared abstract
14+
return "Running fast";
1515
}
1616

17-
String correrDevagar(); // COMPILA - método comum, é abstract por padrão, mesmo que de forma implícita
17+
String runSlow(); // COMPILES - common method, is abstract by default, even if implicitly
1818

19-
abstract String correrExtremo(); // COMPILA - método comum, declarado abstract de forma explícita
19+
abstract String runExtreme(); // COMPILES - common method, explicitly declared abstract
2020

21-
abstract static double calculeVelocidade(int d, int t) { // NÃO COMPILA - método static não pode ser declarado abstract
21+
abstract static double calculateSpeed(int d, int t) { // NOT COMPILING - static method cannot be declared abstract
2222
return d / t;
2323
}
2424
}

src/org/j6toj8/languageenhancements/staticdefaultininterfaces/StaticDefaultInInterfaces_AccessModifiers.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
public class StaticDefaultInInterfaces_AccessModifiers {
66

77
// tag::code[]
8-
interface Corredor {
9-
default String correr() { // COMPILA - não há modificador de acesso declarado, é automaticamente público
10-
return "Correndo";
8+
interface Runner {
9+
default String run() { // COMPILES - there is no declared access modifier, it is automatically public
10+
return "Running";
1111
}
12-
public default String correrRapido() { // COMPILA - modificador de acesso público explícito
13-
return "Correndo Rápido";
12+
public default String runFast() { // COMPILES - explicit public access modifier
13+
return "Running fast";
1414
}
15-
protected default String correrDevagar() { // NÃO COMPILA - o método deve ser obrigatoriamente público
16-
return "Correndo Devagar";
15+
protected default String runSlow() { // NOT COMPILING - the method must be public
16+
return "Running slow";
1717
}
18-
private default String correrExtremo() { // NÃO COMPILA - o método deve ser obrigatoriamente público
19-
return "Correndo ao Extremo";
18+
private default String runExtreme() { // NOT COMPILING - the method must be public
19+
return "Running to the extreme";
2020
}
2121

22-
private static double calculeVelocidade(int d, int t) { // NÃO COMPILA - o método deve ser obrigatoriamente público
22+
private static double calculateSpeed(int d, int t) { // NOT COMPILING - the method must be public
2323
return d / t;
2424
}
2525
}

src/org/j6toj8/languageenhancements/staticdefaultininterfaces/StaticDefaultInInterfaces_Complete.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
public class StaticDefaultInInterfaces_Complete {
66

77
// tag::code[]
8-
interface Corredor {
9-
static double calculeVelocidade(int distancia, int tempo) {
10-
return distancia / tempo;
8+
interface Runner {
9+
static double calculateSpeed(int distance, int time) {
10+
return distance / time;
1111
}
1212

13-
default String correr() {
14-
return "Correndo";
13+
default String run() {
14+
return "Running";
1515
}
1616

17-
String correrRapido();
17+
String runFast();
1818
}
1919

20-
static class Pessoa implements Corredor {
20+
static class Person implements Runner {
2121
@Override
22-
public String correrRapido() {
23-
return "Pessoa Correndo Rápido";
22+
public String runFast() {
23+
return "Fast Running Person";
2424
}
2525

2626
public static void main(String[] args) throws IOException {
27-
System.out.println(Corredor.calculeVelocidade(100, 10));
28-
System.out.println(new Pessoa().correr());
29-
System.out.println(new Pessoa().correrRapido());
27+
System.out.println(Runner.calculateSpeed(100, 10));
28+
System.out.println(new Person().run());
29+
System.out.println(new Person().runFast());
3030
}
3131
}
3232
// end::code[]

src/org/j6toj8/languageenhancements/staticdefaultininterfaces/StaticDefaultInInterfaces_Default.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
public class StaticDefaultInInterfaces_Default {
66

77
// tag::code[]
8-
interface Corredor {
9-
default String correr() {
10-
return "Correndo";
8+
interface Runner {
9+
default String run() {
10+
return "Running";
1111
}
1212
}
1313

14-
static class Pessoa implements Corredor {
14+
static class Person implements Runner {
1515

1616
}
1717

18-
static class Cavalo implements Corredor {
18+
static class Horse implements Runner {
1919
@Override
20-
public String correr() {
21-
return "Galopando";
20+
public String run() {
21+
return "Galloping";
2222
}
2323

2424
public static void main(String[] args) throws IOException {
25-
System.out.println(new Pessoa().correr());
26-
System.out.println(new Cavalo().correr());
25+
System.out.println(new Person().run());
26+
System.out.println(new Horse().run());
2727
}
2828
}
2929
// end::code[]

src/org/j6toj8/languageenhancements/staticdefaultininterfaces/StaticDefaultInInterfaces_InterfaceInheritance.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
public class StaticDefaultInInterfaces_InterfaceInheritance {
44

55
// tag::code[]
6-
interface Corredor {
7-
default String correr() {
8-
return "Correndo";
6+
interface Runner {
7+
default String run() {
8+
return "Running";
99
}
10-
default String correrRapido() {
11-
return "Correndo Rápido";
10+
default String runFast() {
11+
return "Running Fast";
1212
}
13-
default String correrDevagar() {
14-
return "Correndo Devagar";
13+
default String runSlow() {
14+
return "Running Slow";
1515
}
1616
}
1717

18-
interface Piloto extends Corredor {
19-
String correrRapido();
18+
interface Pilot extends Runner {
19+
String runFast();
2020

21-
default String correrDevagar() {
22-
return "Piloto Correndo Devagar";
21+
default String runSlow() {
22+
return "Pilot Running Slow";
2323
}
2424
}
2525
// end::code[]

src/org/j6toj8/languageenhancements/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefault.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
public class StaticDefaultInInterfaces_RepeatedDefault {
44

55
// tag::code[]
6-
interface Corredor {
7-
default String correr() {
8-
return "Correndo";
6+
interface Runner {
7+
default String run() {
8+
return "Running";
99
}
1010
}
1111

12-
interface Piloto {
13-
default String correr() {
14-
return "Piloto Correndo";
12+
interface Pilot {
13+
default String run() {
14+
return "Pilot Running";
1515
}
1616
}
1717

18-
static class Pessoa implements Corredor, Piloto { // NÃO COMPILA - implementa duas interfaces com métodos repetidos e não sobrescreve
18+
static class Person implements Runner, Pilot { // NOT COMPILING - implements two interfaces with repeated methods and does not overwrite
1919

2020
}
2121

22-
static class Gigante implements Corredor, Piloto { // COMPILA - implementa duas interfaces com métodos repetidos, mas sobrescreve e cria sua própria implementação
22+
static class Giant implements Runner, Pilot { // COMPILES - implements two interfaces with repeated methods, but overwrites and creates its own implementation
2323
@Override
24-
public String correr() {
25-
return "Gigante Correndo";
24+
public String run() {
25+
return "Giant Running";
2626
}
2727
}
2828
// end::code[]

0 commit comments

Comments
 (0)