Methods store a block of code and can be called in the program for certain functioning. In Java Overloading and Overriding are the two main concepts in methods. Writing two or more methods using the same name of the method in the same class is called Method overloading. But here passing of parameters is different. Using of the method names in different classes is called Method overriding. Here, child class uses the methods of the parent class. Both of these methods have different purposes to implement. Let us check out some of the main differences between them.
Overloading vs. Overriding in Java
1. Execution is done at the compile-time for overloading whereas, for Overriding, it happens at runtime. What this means is, at compile-time overloaded method call is bound to its definition whereas, for overridden method call, binding to its definition happens at runtime.
2. Overloading can be done for Static methods also, that means, there can be a class which consists of one or more static methods with the same name. But, overriding cannot be done on Static methods. Because there is no use of the child class being declared with a same static method in the parent class.
3. Both the child and the base or parent classes are required for the method overriding, whereas it can be done in the same class in the case of overloading. To the child or the inherited method of the parent class, a specific implementation is given in the overriding
4. Overriding methods use dynamic binding, whereas Static binding is being used for overloaded methods.
5. Performance: Regarding the performance, Overloading always stands first when compared to the overriding. This is because; in the runtime overridden methods are bound.
6. Overridden cannot be done on final and private methods, whereas overloading can be done on them. It means that overriding of final or private methods in a parent class cannot be overridden by the child class. But, one or more final/private methods can be used with the same name in a class.
7. In overloading, a list of the Arguments is, whereas, in Method Overriding, Argument list should be same.
8. Method overriding can be termed as a force behind polymorphism driving, which is a run-time phenomenon. But as we have seen, overloading has a phenomenon of a compile-time.
9. Here are some other simple differences between them. The number or the types of the parameters used by a method have to be changed which belongs to the same class in overloading. Change in the method which is inherited from the parent class will lead to the change in Overriding.
Though there are many other significant differences between these are the main ones you need to know. Each of them has their purposes and can be used in different situations. They are used for portability of the code and allow code re-usability. It is necessary to have knowledge in these topics to build different methods. Try applying them to your programs.