Function override-overload in Java

后端 未结 6 2141
庸人自扰
庸人自扰 2020-12-05 21:18

What is the difference between override and overload?

6条回答
  •  抹茶落季
    2020-12-05 21:44

    Overloading :

    public Bar foo(int some);
    public Bar foo(int some, boolean x);  // Same method name, different signature.
    

    Overriding :

    public Bar foo(int some);   // Defined in some class A
    public Bar foo(int some);   // Same method name and signature. Defined in subclass of A.
    

    If the second method was not defined it would have inherited the first method. Now it will be replaced by the second method in the subclass of A.

提交回复
热议问题