Static methods and their overriding

前端 未结 5 1749
挽巷
挽巷 2020-12-03 06:36

Java doesn\'t allow overriding of static methods but,

class stat13
{
    static void show()
    {
        System.out.println(\"Static in base\");
    }
    p         


        
5条回答
  •  长情又很酷
    2020-12-03 06:53

    No, you're not overriding anything - you're just hiding the original method.

    It's unfortunate that Java allows you to call a static method via a reference. Your call it more simply written as:

    next.show();
    

    Importantly, this code would still call the original version in stat13:

    public static void showStat(stat13 x)
    {
        x.show();
    }
    ...
    showStat(new next());
    

    In other words, the binding to the right method is done at compile-time, and has nothing to do with the value of x - which it would normally with overriding.

提交回复
热议问题