LoginSignup
13
10

More than 5 years have passed since last update.

staticメソッドのオーバーライド

Last updated at Posted at 2016-06-17

staticメソッドはクラス固有のものであるためオーバーライドができない(下記例)。

public class Main {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.hoge();
        obj.piyo();
    }
}

class Parent {
    public static void hoge() {
        System.out.println("parent");
    }

    public void piyo() {
        System.out.println("parent");
    }
}

class Child extends Parent {
    // もちろん@Overrideアノテーションを付けるとコンパイルエラー
    public static void hoge() {
        System.out.println("child");
    }

    @Override
    public void piyo() {
        System.out.println("child");
    }
}
# 実行結果
parent
child
13
10
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10