Search code examples
javastaticstatic-methods

Java Why T is still calling default method when T extends the interface


public class Draw<T extends Print> (Print is a interface)

Other classes:

public interface Print {
    static void info(String message) {
        System.out.println("failed");
    }
 }
public class Test implements Print {
    static void info(String message) {
        System.out.println(message);
    }
}

When use new Draw<Test>() and try to run T.info("Test"); it only printed "failed"

Am I did anything wrong?


Solution

  • Static methods don’t get overridden. You have to call Test.info to call your method. Static methods are on interfaces because it’s a convenient place to put them. It’s not something you can override, and it’s not a default method. Default methods aren’t static, and you have to use the default keyword.

    If you want polymorphism, use instance methods, not static ones.