Overriding vs Overloading
The method Overriding and method Overloading are two concepts/techniques/feature found in some programming languages. Both concepts allow the programmer to provide different implementations for methods with the same name. Method overriding allows the programmer to provide an alternative implementation within a sub class to a method already defined inside its super class. Method overloading allows the programmer to provide different implementations to multiple methods with the same name (within the same class).
What is Overriding?
As mentioned above, a class can extend a super class or a parent class, in object oriented programming languages. A child class can have its own methods or can optionally have its own implementations to methods already defined in its parent class (or one of its grand parent classes). So when the latter happens, it is called method overriding. In other words, if the child class provides an implementation to a method with the same signature and return type as a method already defined in one of its parent classes, that method is said to be overridden (replaced) by the implementation of the child class. So, if there is an overridden method in a class, the runtime system will have to decide which method implementation is used. This issue is resolved by looking at the exact type of object that is used to invoke it. If an object of the parent class is used to invoke the overridden method, then the implementation in the parent class is used. Similarly, if it is an object of the child class that is used, then the child class’s implementation is used. Modern programming languages like Java, Eifell, C++ and Python allows method overriding.
What is Overloading?
Method overloading is a feature provided by some programming languages to create more than one method with the same name, but with different input and output types. In modern programming languages like Java, C#, C++ and VB.NET, this feature is available. You can overload a method by creating another method with the same name but with a different method signature or a different return type (or both). For example, if you have method1(type1 t1) and method1(type2 t2) inside the same class, then they are overloaded. Then the system will have to decide which one to be executed when it is called. This differentiation is made by looking at the type of the parameter(s) been passed in to the method. If the argument is of type1, then the first implementation is called, while if it is of type2, then the second implementation is called.
What is the difference between Overriding and Overloading?
Although, method overriding and method overloading are used to provide a method with different implementations, there are key differences between these two concepts/techniques. First of all, subjects of method overriding always stay within different classes, while subjects of method overloading stay within the same class. That means overriding is only possible in object oriented programming languages that allows inheritance, while overloading can be available in a non object-oriented language as well. In other words, you override a method in the super class but you overload a method within your own class.
Another difference is that overridden methods have the same method name, method signature and the return type, but overloaded methods must differ in either the signature or the return type (the name should be the same). In order to differentiate between two overridden methods, the exact type of object that is used to invoke the methods id used, whereas to differentiate between two overloaded methods the types of the parameters are used. Another key difference is that overloading is resolved at compile time, while overriding is resolved at runtime.