</>

Technology

Java Programs

Difficulty

Intermediate

Interview Question

Write a Java program to demonstrate method overloading and method overriding (polymorphism).

Answer

Method Overloading vs Method Overriding

Method Overloading (Compile-Time / Static Polymorphism)

Java
public class MethodOverloading {

    // Same method name, different parameter types/count
    int add(int a, int b) {
        return a + b;
    }

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

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

    String add(String a, String b) {
        return a + b;     // string concatenation
    }

    public static void main(String[] args) {
        MethodOverloading obj = new MethodOverloading();

        System.out.println(obj.add(2, 3));           // 5       → int version
        System.out.println(obj.add(2.5, 3.5));       // 6.0     → double version
        System.out.println(obj.add(1, 2, 3));        // 6       → three-param version
        System.out.println(obj.add("Hello", "World")); // HelloWorld → String version
    }
}

Output

CODE
5
6.0
6
HelloWorld

Method Overriding (Runtime / Dynamic Polymorphism)

Java
class Shape {
    String color = "White";

    void draw() {
        System.out.println("Drawing a shape in " + color);
    }

    double area() {
        return 0;
    }
}

class Circle extends Shape {
    double radius;

    Circle(double r) { this.radius = r; }

    @Override
    void draw() {   // overrides Shape.draw()
        System.out.println("Drawing a CIRCLE with radius " + radius);
    }

    @Override
    double area() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    double width, height;

    Rectangle(double w, double h) { this.width = w; this.height = h; }

    @Override
    void draw() {
        System.out.println("Drawing a RECTANGLE " + width + "x" + height);
    }

    @Override
    double area() {
        return width * height;
    }
}

public class MethodOverriding {
    public static void main(String[] args) {
        // Runtime polymorphism — which draw() is called determined at runtime
        Shape s1 = new Circle(5.0);
        Shape s2 = new Rectangle(4.0, 6.0);

        s1.draw();    // Circle's draw()
        s2.draw();    // Rectangle's draw()

        System.out.printf("Circle area:    %.2f%n", s1.area());
        System.out.printf("Rectangle area: %.2f%n", s2.area());
    }
}

Output

CODE
Drawing a CIRCLE with radius 5.0
Drawing a RECTANGLE 4.0x6.0
Circle area:    78.54
Rectangle area: 24.00

Overloading vs Overriding — Key Differences

Method OverloadingMethod Overriding
ClassSame classParent + Child class
ParametersMust differMust be same
Return typeCan differMust be same (or covariant)
BindingCompile time (static)Runtime (dynamic)
PolymorphismStatic / compile-timeDynamic / runtime
@OverrideNot neededRecommended
private/staticCan overloadCannot override

Common Interview Question: Can we override static methods?

Java
class Parent {
    static void display() { System.out.println("Parent static"); }
    void show()           { System.out.println("Parent instance"); }
}

class Child extends Parent {
    // This is METHOD HIDING, NOT overriding
    static void display() { System.out.println("Child static"); }

    @Override
    void show() { System.out.println("Child instance"); }  // true override
}

Parent obj = new Child();
obj.display();  // "Parent static" — static: resolved at compile time
obj.show();     // "Child instance" — instance: resolved at runtime

Follow AutomateQA