Difference between Method Overloading and Overriding in Java? Answer

Overloading vs Overriding in Java
In the last couple of articles, we have seen What is method overloading and What is method overriding in Java and now we will see What is difference between overloading and overriding in Java. Overloading vs overriding is one of those frequently asked Java interview questions which can not be ignored. Overloading vs Overriding has appeared in almost every Java interview, mostly at beginner and intermediate level like 2 to 4 years experience. In fact, most of those tricky Java interview Questions came from Overloading and Overriding. 

It's one of the tricky fundamentals to understand. In this article, we will some important difference between Overloading and Overriding which not only help to understand the concept better but also serves as a good recap for Java interviews.


Difference between Method Overloading and Overriding in Java

Here are some of the most common differences between both of them. If you are working in Java for more than 1 year, you might be familiar with all of them but anyway it's a good revision :


1. Timing
The first and major difference between Overloading and Overriding is that the former occurs during compile time while the latter occurs during runtime.


2.Location
The second difference between Overloading and Overriding is that you can overload a method in the same class but you can only override a method in a subclass.


3. Static modifier
The third difference is that you can overload the static method in Java but you can not override the static method in Java. In fact, when you declare the same method in Sub Class it's known as method hiding because it hides the superclass method instead of overriding it.


4. Resolution
Overloaded methods are bonded using static binding and the type of reference variable is used, while the Overridden method is bonded using dynamic bonding based upon the actual Objects.


5. Rules
Rules of Overloading and Overriding are different in Java. In order to overload a method you need to change its method signature but that is not required for overriding any method in Java.

6. private and final methods
Another difference between method overloading and overriding is that private and final methods can not be overridden but can be overloaded in Java.


7. Speed
The overloaded method is fast as compare to the Overridden method in Java.

And here is a diagram worth 1000 words to explain the overloading vs overriding concept in Java:

Difference between Overloading and Overriding in Java? Answer

Method Overloading and Overriding example in Java

There is no better way to understand a concept then by seeing an example. Method overloading and method overriding are two fundamental concepts in object-oriented programming, particularly in languages like Java.

Let's explore each with examples in Java:

1. Method Overloading Example

Method overloading allows you to define multiple methods in a class with the same name but different parameter lists (i.e., a different number or type of parameters). The compiler determines which method to call based on the arguments passed during the method invocation.

Here's an example of method overloading in Java:

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

    // Method to add three integers (overloaded version)
    public int add(int a, int b, int c) {
        return a + b + c;
    }

    // Method to add two doubles (overloaded version)
    public double add(double a, double b) {
        return a + b;
    }
}

public class MethodOverloadingExample {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();

        int sum1 = calculator.add(10, 20);
        int sum2 = calculator.add(10, 20, 30);
        double sum3 = calculator.add(5.5, 3.7);

        System.out.println("Sum1: " + sum1);
        System.out.println("Sum2: " + sum2);
        System.out.println("Sum3: " + sum3);
    }
}

In this example, the Calculator class defines multiple add methods with different parameter lists. The appropriate method is invoked based on the number and types of arguments passed when calling the add method.

2. Method Overriding Example

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The overridden method in the subclass must have the same method signature (method name, return type, and parameters) as the method in the superclass.

Here's an example of method overriding in Java:

class Animal {
    public void makeSound() {
        System.out.println("Animal makes a generic sound");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat meows");
    }
}

public class MethodOverridingExample {
    public static void main(String[] args) {
        Animal animal1 = new Dog();
        Animal animal2 = new Cat();

        animal1.makeSound(); // Calls Dog's makeSound method
        animal2.makeSound(); // Calls Cat's makeSound method
    }
}

In this example, the Animal class has a makeSound method. The Dog and Cat classes are subclasses of Animal and override the makeSound method to provide their specific implementations.

When we create instances of Dog and Cat and call the makeSound() method, the overridden methods in the respective subclasses are invoked, demonstrating method overriding.

So, you can see that even though the name of the method remains same their behavior is totally different in case of overloading and overriding. 

That's all on the difference between method overloading and overriding in Java. Apart from rules of overloading and overriding, these are some important differences that are worth remembering while overloading or overriding any method in Java.


Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

Now, its quiz time so one question for? Can we override a static or private method in Java? Can you explain with a reason?

3 comments:

  1. I think point 2 is incorrect because we can overload a method in sub class also http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.9

    ReplyDelete
    Replies
    1. Yes, correct, you can overload the method on sub-class as well as long as the method signature is different. As Java specification says "If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded."

      Delete

Feel free to comment, ask questions if you have any doubt.