Python Overloading | Overloading in python
Overloading, in the context of programming, refers to the ability of a function or an operator to behave in different ways depending on the parameters that are passed to the function, or the operands that the operator acts on.
Overloading a method fosters reusability. For instance, instead of writing multiple methods that differ only slightly, we can write one method and overload it. Overloading also improves code clarity and eliminates complexity.
Like other languages do, python does not supports method overloading. We may overload the methods but can only use the latest defined method.
In the above code we have defined two abc method, but we can only use the second product method, as python does not supports method overloading. We may define many method of same name and different argument but we can only use the latest defined method. Calling the other method will produce an error. Like here calling abc(10,20) will produce an error as the latest defined abc method takes three arguments.
Python allows us to change the default behavior of an operator depending on the operands that we use. This practice is referred to as "operator overloading".
The functionality of Python operators depends on built-in classes. However, the same operator will behave differently when applied to different types. A good example is the "+" operator. This operator will perform an arithmetic operation when applied on two numbers, will concatenate two strings, and will merge two lists.
Method overriding, in object-oriented programming, 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 super classes or parent classes. The implementation in the subclass overrides (replaces) the implementation in the super class by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.
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.
In this example we create the object of parent class A and access the method using parent class object