Function override-overload in Java

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

What is the difference between override and overload?

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 21:35

    On interesting thing to mention:

    public static doSomething(Collection c) {
        // do something
    }
    
    public static doSomething(ArrayList l) {
        // do something
    }
    
    public static void main(String[] args) {
        Collection c = new ArrayList ();
        doSomething(c); // which method get's called?
    }
    

    One would suppose the method with the ArrayList argument would be called but it doesn't. The first method is called since the proper method is selected at compile time.

提交回复
热议问题