Overloading vs Overriding

Overloading vs Overriding

Overloading and Overriding are the two very important concept of Object Oriented Programming. But, most of the novice programmers are confused about them or they forgot the rules on when to use which one. In this article, we will discuss about the overloading and overriding briefly.

So, lets get started-

Overloading

Method overloading is the ability to create multiple functions of the same name with different implementations. An overloaded function will run a specific implementation that allowing one function call to perform different tasks depending on context.

In OOPs, overloading plays an important role, which lets us define multiple methods with same name inside a class only number of parameters should be different. Overloaded methods are generally used to execute the same task, but with a different set of parameters. The actual method called during runtime is resolved at compile time, which avoid runtime errors. The concept of overloading helps to avoid redundant code, eliminates complexity, and enhances runtime performance.

Overriding

Method overriding is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.

The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has the same name, same parameters or signature, and same return type as the method in the parent class.

Whenever we extend a parent class in a child class, the child class able to access all the methods, which is called derived methods. But in some cases, if we do not want the same parent method, then we need the concept of overriding. Overriding let us implement a parent class method in a child class with the same name and parameters. It will depend on the object which version of the method will be called during runtime. If the object is a shadow of the parent class, then method of the parent class will be called, while child class object will execute the class version of the method.

The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. Some languages allow a programmer to prevent a method from being overridden.

C++ and C# support overriding by explicitly using the keywords 'override' and 'virtual'. Java uses the 'super' keyword to invoke the superclass method. However, C++ uses instead the base class name followed by the scope resolution operator (::).

Difference between Overloading and Overriding

From the above definition, we can easily figure out the base difference between overloading and overriding. Now, let’s dive into the depth.

  1. Overloading happens in the same class, but overriding only possible for deriving method.

  2. Overloading happens at compile-time while overriding happens at runtime.

  3. Static methods can be overloaded, but static methods cannot be overridden. If you declare a same static method in child class it has nothing to do with the same method of the parent class.

  4. Static binding is used for overloading a method and dynamic binding is used for method overriding.

  5. In the case of performance, method overloading gives better performance compared to override because of the binding.

  6. Return type doesn’t matter for method overloading only parameter must be same, but the overriding method can have a more specific return type.

  7. Final and private methods can also be overloaded, but they cannot be overridden.

  8. Polymorphism applies to override, but not for overloading.

  9. In function overloading, the same function name is used for more than one function definition. The functions must differ either by the arity or types of their parameters.

  10. When overriding a method with another, the signatures of the two methods must be identical.

  11. Function overloading is usually associated with statically-typed programming languages that enforce type checking in function calls.

  12. Non-virtual or static methods cannot be overridden. The overridden base method must be virtual, abstract, or override.

  13. Overloading is used when you want to extend the functionality. Overriding is used when you want to reuse the existing functionality.

 

Hope this article will help you to learn more about the concept of OOP. Share your thought in the comment section below.

Recommended for you