Last active
May 19, 2021 20:41
-
-
Save alexandreaquiles/10272679 to your computer and use it in GitHub Desktop.
Exemplo de colisão de métodos default no Java 8.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Aquatico { | |
default void move(){ | |
System.out.println("Move na água..."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Aviao { | |
public void move(){ | |
System.out.println("Voa..."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see http://pt.wikipedia.org/wiki/Hidroavi%C3%A3o | |
*/ | |
class HidroAviao extends Aviao implements Aquatico { | |
//nao precisa implementar nem sobreescrever... | |
//O método da superclasse é escolhido | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see http://pt.wikipedia.org/wiki/Hovercraft | |
*/ | |
class Hovercraft implements Aquatico, Terrestre { | |
public void move(){ | |
System.out.println("Move na água e na terra..."); | |
Aquatico.super.move(); | |
Terrestre.super.move(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HovercraftComPau implements Aquatico, Terrestre { | |
//^ | |
//error: class HovercraftComPau inherits unrelated defaults for move() from types Aquatico and Terrestre | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Programa { | |
public static void main(String[] args){ | |
Hovercraft hover = new Hovercraft(); | |
hover.move(); | |
//imprime: | |
// Move na água e na terra... | |
// Move na água... | |
// Move na terra... | |
HidroAviao hidro = new HidroAviao(); | |
hidro.move(); | |
//imprime: | |
// Voa... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Terrestre { | |
default void move(){ | |
System.out.println("Move na terra..."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mais claro impossivel :). obrigado alexandre