Overload (多載) vs. Override (覆寫)
Mar 23, 2021
Overloading(多載): occurs when two or more methods in one class have the same method name but different parameters.
一個類別(class)中可以有多個名稱相同,但參數(Parameter)不同的方法(Method)。會根據你帶入的參數型態與數量不同,去找出相對應的functoin
ex.
- void show(int i)
- void show(double i)
- void show(int i, int j)
Overriding(覆寫): having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided by its parent class.
子類別可以覆寫父類別的內容,使該function有不同的內容。