The method overloading vs overriding in Java

Method Overloading

public class CalculatorUtil {   public static int sum(int i, int i2) {
return i + i2;
}
public static int sum(int i, int i2, int i3) {
return i + i2 + i3;
}

public static double sum(double i, double i2) {
return i + i2;
}

public static double sum(double i, double i2, double i3) {
return i + i2 + i3;
}
}

In the above example, we have four overloaded methods. It’s convenient because when the client code will use our sum they won't think should we pass int or double.

One of the most popular method

System.out.println();

has 10 overloaded methods

Did you know about that?

It is super useful for us. When we use it we just know it will print output in the console and we can pass everything we wanted to print.

Method Overriding

Method overriding when a child or concrete class overrides its parent method. We can override our parent class methods when we extend them and we can override methods of interfaces when we implement them(we implement abstract methods by overriding them).

Rules of overriding:

That’s all I have for today. Have a nice day!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store