Difference between overriding and overloading in Java?
Overriding | Overloading |
---|---|
In overriding, method names must be the same. | In overloading, method names must be the same. |
The argument list must be the same. | The argument list must be different, at least in the order of arguments. |
The return type can be the same or a covariant type. Covariant types have been allowed since Java 1.5. | The return type can be different in overloading. |
The level of checked exceptions cannot be increased. | There are no restrictions for unchecked exceptions. |
Different exceptions can be thrown in overloading. | A method can only be overridden in a subclass. |
Private, static, and final variables cannot be overridden. | Private, static, and final variables can be overloaded. |
In overriding, the method to be called is decided at runtime based on the type of object referenced at runtime. | In overloading, the method to call is decided at compile time-based on the reference type. |
Overriding is also known as runtime polymorphism, dynamic polymorphism, or late binding. | Overloading is also known as compile-time polymorphism, static polymorphism, or early binding. |