Last active
August 29, 2015 13:58
-
-
Save alexandreaquiles/10394860 to your computer and use it in GitHub Desktop.
Métodos estáticos de interfaces só podem ser acessados diretamente pela interface.
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 Interface { | |
static void i(){ | |
System.out.println("interface"); | |
} | |
} | |
class ClasseMae { | |
static void a(){ | |
System.out.println("mãe"); | |
} | |
} | |
class ClasseFilha extends ClasseMae implements Interface { | |
static void b(){ | |
System.out.println("filha"); | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
ClasseMae a = new ClasseFilha(); | |
a.a(); //dá warning no Eclipse: "The static method a() from the type ClasseMae should be accessed in a static way" | |
a.b(); //obviamente, dá erro | |
//no Eclipse: "The method b() is undefined for the type ClasseMae" | |
//no javac: "error: cannot find symbol / symbol: method b() / location: variable a of type ClasseMae" | |
Interface i = new ClasseFilha(); | |
i.i(); //estranhamente, dá erro. | |
//no Ecipse: This static method of interface Interface can only be accessed as Interface.i | |
//no javac: "error: illegal static interface method call / the receiver expression should be replaced with the type qualifier 'Interface'" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Achei interessante dar esse erro :). Para min deveria dar erro de compilação para qualquer acesso de método de estático como se fosse método de instância :).