LeetCampus
Interview Question

Comment on method overloading and overriding by citing relevant examples.

July 24, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

Method overloading and method overriding are fundamental concepts in object-oriented programming that allow for more flexible and reusable code. Interviewers often ask about these concepts to assess a candidate's understanding of polymorphism, which is a core principle in OOP. Understanding these concepts is crucial because they directly impact how methods are executed in different contexts, thus influencing the design and functionality of applications. Method overloading allows multiple methods with the same name but different parameters, enhancing readability and usability. For example, it can facilitate different behaviors based on input types. In contrast, method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass, enabling dynamic method resolution. This is essential for achieving runtime polymorphism. Interviewers may also be looking for common misconceptions, such as confusing the two concepts or misunderstanding their practical applications in real-world scenarios. Overall, a solid grasp of method overloading and overriding is essential for effective software design and development.

Sample Answers

Example 1: Method Overloading in Java

Consider a simple Java class that demonstrates method overloading:

public class Calculator {
    // Method to add two integers
    public int add(int a, int b) {
        return a + b;
    }

    // Overloaded method to add three integers
    public int add(int a, int b, int c) {
        return a + b + c;
    }

    // Overloaded method to add two double values
    public double add(double a, double b) {
        return a + b;
    }
}

In this example, the add method is overloaded with different parameter counts and types. This allows the same method name to be used for different operations, enhancing code clarity. The compiler differentiates between these methods based on the method signatures, enabling the correct one to be invoked based on the arguments provided. This technique is particularly useful in creating APIs that are intuitive and easy to use. Method overloading is a form of compile-time polymorphism, as the method to be executed is determined at compile time.

Example 2: Method Overriding in Python

In Python, method overriding is achieved when a subclass provides a specific implementation of a method that is already defined in its superclass. Here's an example:

class Animal:
    def sound(self):
        return 'Some sound'

class Dog(Animal):
    def sound(self):
        return 'Bark'

class Cat(Animal):
    def sound(self):
        return 'Meow'

In this case, the Animal class has a method sound, which is overridden by both Dog and Cat classes. When you create an instance of Dog or Cat and call the sound method, the overridden version specific to that subclass will be executed:

my_dog = Dog()
print(my_dog.sound())  # Outputs: Bark

my_cat = Cat()
print(my_cat.sound())  # Outputs: Meow

This showcases runtime polymorphism, where the method that gets executed is determined at runtime based on the object type. Overriding is useful for implementing functionality that is specific to subclasses while maintaining a common interface.

Example 3: Practical Application of Both Concepts

Imagine you are designing a graphics application that involves shapes. You can use both method overloading and method overriding effectively. For instance, consider a Shape class:

public class Shape {
    public void draw() {
        System.out.println("Drawing a shape.");
    }
    public void draw(int radius) {
        System.out.println("Drawing a circle with radius: " + radius);
    }
}

public class Square extends Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a square.");
    }
}

Here, the Shape class has an overloaded draw method that can draw a generic shape or a circle based on the provided parameter. The Square class overrides the draw method to specify how to draw a square. This design allows the application to call draw on any Shape object and receive the appropriate behavior:

Shape s1 = new Shape();
s1.draw(); // Outputs: Drawing a shape.
Shape s2 = new Square();
s2.draw(); // Outputs: Drawing a square.

By leveraging both concepts, you ensure that your code is flexible, maintainable, and adheres to the principles of object-oriented design.

Keywords

method overloadingmethod overridingpolymorphismobject-oriented programmingsoftware engineering

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions