Skip to content

Commit

Permalink
feat(lang-enh): 馃幐 static/default methods interface, translated
Browse files Browse the repository at this point in the history
Refers: #4
  • Loading branch information
rcmoutinho committed Sep 11, 2019
1 parent 81639b9 commit 6159564
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -1,126 +1,122 @@
:java-package: src/org/j6toj8/languageenhancements
:section-java-package: ../../../{java-package}

=== M茅todos `static` e `default` em Interfaces
=== Static and default methods of an interface

.Objetivo
.Objective
----
Use static and default methods of an interface including inheritance rules for a default method.
-
Usar m茅todos static e default de uma interface, incluindo regras de heran莽a para um m茅todo default.
----

脡 esperado que o candidato saiba compreender e analisar o uso da dos modificadores `static` e `default` em m茅todos de interfaces.
It is expected that the candidate can understand and analyze the use of `static` and `default` modifiers in interface methods.

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.
Before proceeding, based on the following example, understand the execution of the `main` method and what is presented on the console after its execution.

[source,java,indent=0]
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Complete.java
----
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Complete.java[tag=code]
----

.Sa铆da no console
.console output
[source,console]
----
10.0
Correndo
Pessoa Correndo R谩pido
Running
Fast Running Person
----

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

. Desde o Java 8, Interfaces podem ter m茅todos com o modificador `static`.
. Since Java 8, Interfaces can have methods with the `static` modifier.

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

. Desde o Java 8, Interfaces podem ter m茅todos com o modificador `default`.

. M茅todos `default` n茫o precisam, mas podem, ser sobrescritos.
. Since Java 8, Interfaces can have methods with the `default` modifier.

. `Default` methods do not need, but can be overridden.
+
[source,java,indent=0]
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Default.java
----
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_Default.java[tag=code]
----
+
Veja que a classe `Pessoa` n茫o sobrescreve o m茅todo `correr()`, mantendo o comportamento padr茫o da implementa莽茫o feita na interface `Corredor`.
Note that the `Person` class does not override the `run()` method, maintaining the default implementation behavior of the `Runner` interface.
+
A classe `Cavalo`, por outro lado, sobrescreve o m茅todo `correr()` para ter sua pr贸pria implementa莽茫o.
The `Horse` class, on the other hand, overrides the `run()` method to have its own implementation.
+
.Sa铆da no console
.console output
[source,console]
----
Correndo
Galopando
Running
Galloping
----

. 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`.
. Like the other methods of an interface, the `static` and `default` *methods are always `public`*, and cannot be changed to `private` or `protected`.
+
[source,java,indent=0]
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_AccessModifiers.java
----
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_AccessModifiers.java[tag=code]
----

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

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

. Se uma classe implementa duas interfaces que possuem m茅todos `default` repetidos, ela obrigatoriamente deve implementar o seu pr贸prio.
. If a class implements two interfaces that have repeated `default` methods, it must implement its own.
+
[source,java,indent=0]
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefault.java
----
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefault.java[tag=code]
----

. Ao implementar m煤ltiplas interfaces, 茅 poss铆vel acessar a implementa莽茫o `default` de uma delas.
. By implementing multiple interfaces, you can access the `default` implementation of one of them.
+
[source,java,indent=0]
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefaultSuper.java
----
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_RepeatedDefaultSuper.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
Correndo
Running
----

. Quando uma interface herda de outra interface m茅todos `default`, estes podem ser mantidos, transformados em `abstract` ou redefinidos.
. When an interface inherits from another interface, `default` methods can be retained, transformed into `abstract` or redefined.
+
[source,java,indent=0]
.{java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_InterfaceInheritance.java
----
include::{section-java-package}/staticdefaultininterfaces/StaticDefaultInInterfaces_InterfaceInheritance.java[tag=code]
----
+
Nesse exemplo, a interface `Piloto` herda de `Corredor` e mostra 3 cen谩rios distintos:
In this example, the `Pilot` interface inherits from `Runner` and shows 3 distinct scenarios:

* Mant茅m o m茅todo `correr()` inalterado;
* 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;
* Altera o m茅todo `correrDevagar()` para que tenha sua pr贸pria implementa莽茫o
* Keep `run()` method unchanged;
* Changes the `runFast()` method to be `abstract`, so any class that implements the `Pilot` interface has to implement this method;
* Change `runSlow()` method to have its own implementation

.Refer锚ncias
.References
****
* Designing an Interface
+
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 48). Wiley. Edi莽茫o do Kindle.
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 48). Wiley. Kindle Edition.
* https://www.baeldung.com/java-static-default-methods[Static and Default Methods in Interfaces in Java.]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
public class StaticDefaultInInterfaces_Abstract {

// tag::code[]
interface Corredor {
default String correr() { // COMPILA - m茅todo default n茫o 茅 abstract
return "Correndo";
interface Runner {
default String run() { // COMPILES - default method is not abstract
return "Running";
}

abstract default String correrRapido() { // N脙O COMPILA - m茅todo default n茫o pode ser declarado abstract
return "Correndo R谩pido";
abstract default String runFast() { // NOT COMPILING - default method cannot be declared abstract
return "Running fast";
}

String correrDevagar(); // COMPILA - m茅todo comum, 茅 abstract por padr茫o, mesmo que de forma impl铆cita
String runSlow(); // COMPILES - common method, is abstract by default, even if implicitly

abstract String correrExtremo(); // COMPILA - m茅todo comum, declarado abstract de forma expl铆cita
abstract String runExtreme(); // COMPILES - common method, explicitly declared abstract

abstract static double calculeVelocidade(int d, int t) { // N脙O COMPILA - m茅todo static n茫o pode ser declarado abstract
abstract static double calculateSpeed(int d, int t) { // NOT COMPILING - static method cannot be declared abstract
return d / t;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
public class StaticDefaultInInterfaces_AccessModifiers {

// tag::code[]
interface Corredor {
default String correr() { // COMPILA - n茫o h谩 modificador de acesso declarado, 茅 automaticamente p煤blico
return "Correndo";
interface Runner {
default String run() { // COMPILES - there is no declared access modifier, it is automatically public
return "Running";
}
public default String correrRapido() { // COMPILA - modificador de acesso p煤blico expl铆cito
return "Correndo R谩pido";
public default String runFast() { // COMPILES - explicit public access modifier
return "Running fast";
}
protected default String correrDevagar() { // N脙O COMPILA - o m茅todo deve ser obrigatoriamente p煤blico
return "Correndo Devagar";
protected default String runSlow() { // NOT COMPILING - the method must be public
return "Running slow";
}
private default String correrExtremo() { // N脙O COMPILA - o m茅todo deve ser obrigatoriamente p煤blico
return "Correndo ao Extremo";
private default String runExtreme() { // NOT COMPILING - the method must be public
return "Running to the extreme";
}

private static double calculeVelocidade(int d, int t) { // N脙O COMPILA - o m茅todo deve ser obrigatoriamente p煤blico
private static double calculateSpeed(int d, int t) { // NOT COMPILING - the method must be public
return d / t;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
public class StaticDefaultInInterfaces_Complete {

// tag::code[]
interface Corredor {
static double calculeVelocidade(int distancia, int tempo) {
return distancia / tempo;
interface Runner {
static double calculateSpeed(int distance, int time) {
return distance / time;
}

default String correr() {
return "Correndo";
default String run() {
return "Running";
}

String correrRapido();
String runFast();
}

static class Pessoa implements Corredor {
static class Person implements Runner {
@Override
public String correrRapido() {
return "Pessoa Correndo R谩pido";
public String runFast() {
return "Fast Running Person";
}

public static void main(String[] args) throws IOException {
System.out.println(Corredor.calculeVelocidade(100, 10));
System.out.println(new Pessoa().correr());
System.out.println(new Pessoa().correrRapido());
System.out.println(Runner.calculateSpeed(100, 10));
System.out.println(new Person().run());
System.out.println(new Person().runFast());
}
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
public class StaticDefaultInInterfaces_Default {

// tag::code[]
interface Corredor {
default String correr() {
return "Correndo";
interface Runner {
default String run() {
return "Running";
}
}

static class Pessoa implements Corredor {
static class Person implements Runner {

}

static class Cavalo implements Corredor {
static class Horse implements Runner {
@Override
public String correr() {
return "Galopando";
public String run() {
return "Galloping";
}

public static void main(String[] args) throws IOException {
System.out.println(new Pessoa().correr());
System.out.println(new Cavalo().correr());
System.out.println(new Person().run());
System.out.println(new Horse().run());
}
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
public class StaticDefaultInInterfaces_InterfaceInheritance {

// tag::code[]
interface Corredor {
default String correr() {
return "Correndo";
interface Runner {
default String run() {
return "Running";
}
default String correrRapido() {
return "Correndo R谩pido";
default String runFast() {
return "Running Fast";
}
default String correrDevagar() {
return "Correndo Devagar";
default String runSlow() {
return "Running Slow";
}
}

interface Piloto extends Corredor {
String correrRapido();
interface Pilot extends Runner {
String runFast();

default String correrDevagar() {
return "Piloto Correndo Devagar";
default String runSlow() {
return "Pilot Running Slow";
}
}
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
public class StaticDefaultInInterfaces_RepeatedDefault {

// tag::code[]
interface Corredor {
default String correr() {
return "Correndo";
interface Runner {
default String run() {
return "Running";
}
}

interface Piloto {
default String correr() {
return "Piloto Correndo";
interface Pilot {
default String run() {
return "Pilot Running";
}
}

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

}

static class Gigante implements Corredor, Piloto { // COMPILA - implementa duas interfaces com m茅todos repetidos, mas sobrescreve e cria sua pr贸pria implementa莽茫o
static class Giant implements Runner, Pilot { // COMPILES - implements two interfaces with repeated methods, but overwrites and creates its own implementation
@Override
public String correr() {
return "Gigante Correndo";
public String run() {
return "Giant Running";
}
}
// end::code[]
Expand Down
Loading

0 comments on commit 6159564

Please sign in to comment.