Cross_Column

Wednesday, 3 June 2026

Method Overloading Not Supported in Python



Method Overloading in Java vs Python – An OOP Concept Present in Java but Not in Python

Object-Oriented Programming (OOP) provides several powerful concepts such as Encapsulation, Inheritance, Polymorphism, and Abstraction. Both Java and Python support most OOP principles, but there are some differences in how they implement them.

One of the most common interview questions asked to automation testers and developers is:

Which OOP concept is present in Java but not in Python?

The answer is Method Overloading, also known as Compile-Time Polymorphism.


What is Method Overloading?

Method Overloading is a feature that allows a class to have multiple methods with the same name but different parameter lists.

The compiler determines which method should be executed based on the number or type of arguments passed.

Java Example of Method Overloading

class Calculator {

    int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }

    double add(double a, double b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {

        Calculator c = new Calculator();

        System.out.println(c.add(10, 20));
        System.out.println(c.add(10, 20, 30));
        System.out.println(c.add(10.5, 20.5));
    }
}

Output

30
60
31.0

In the above example, the method name add() remains the same, but the parameter list changes. Java compiler automatically identifies which method to execute.


Does Python Support Method Overloading?

No, Python does not support true method overloading.

If multiple methods with the same name are created inside a class, the latest definition overrides the previous ones.

Python Example

class Calculator:

    def add(self, a, b):
        return a + b

    def add(self, a, b, c):
        return a + b + c


obj = Calculator()

print(obj.add(10, 20))

Output

TypeError:
add() missing 1 required positional argument: 'c'

Here, the second add() method replaces the first one. Therefore, Python does not maintain multiple versions of the same method.


How Does Python Achieve Similar Functionality?

Although Python does not support traditional method overloading, similar behavior can be achieved using:

  • *args
  • **kwargs
  • Default Arguments

Using *args

class Calculator:

    def add(self, *args):
        return sum(args)


obj = Calculator()

print(obj.add(10, 20))
print(obj.add(10, 20, 30))
print(obj.add(10, 20, 30, 40))

Output

30
60
100

The *args parameter allows Python methods to accept a variable number of arguments.


Constructor Overloading in Java vs Python

Just like method overloading, Java supports constructor overloading.

Java Example

class Employee {

    Employee() {
        System.out.println("Default Constructor");
    }

    Employee(String name) {
        System.out.println(name);
    }
}

Python does not support constructor overloading directly because a class can have only one __init__() method.

Python Alternative

class Employee:

    def __init__(self, name=None):

        if name:
            print(name)
        else:
            print("Default Constructor")


Employee()
Employee("Chandan")

Other Differences Between Java and Python OOP Concepts

Feature Java Python
Method Overloading Supported Not Supported
Constructor Overloading Supported Not Supported
Access Modifiers Enforcement Strongly Enforced Convention Based
Multiple Inheritance Not Supported for Classes Supported
Interfaces Supported Achieved using Abstract Base Classes and Protocols

Interview Question & Answer

Question

Which OOP concept is present in Java but not in Python?

Answer

Method Overloading (Compile-Time Polymorphism) is supported directly in Java, where multiple methods can have the same name with different parameter lists. Python does not support true method overloading because a class can only have one method with a given name. Similar behavior can be achieved using *args, **kwargs, or default arguments.


Conclusion

Java and Python both support Object-Oriented Programming, but they implement certain features differently. One notable difference is Method Overloading, which is a native feature of Java but not Python. Python provides flexible alternatives such as *args and default arguments to achieve similar functionality.

Understanding these differences is extremely useful for automation testers, SDETs, and developers who work with both Java and Python.

No comments:

Post a Comment

Few More

Python Quiz - Test Your Python Programming Skills Online

Python Quiz - Test Your Python Programming Skills Online Next Question What You'll Learn Variables an...