Method Overloading and Overriding


Classes and Objects

Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.

Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.


Overloading Methods

Sometimes the same functionality has to be written for different kinds of inputs. At that time, one can use the same method name with a different set of parameters. Each different set of parameters is known as a method signature. As seen per the example, a single method can have multiple signatures.

public class NamePrinter {
    public void printFullName(String firstPart) {
        System.out.println("Full Name: " + firstPart);
    }
 
    public void printFullName(String firstPart, String secondPart) {
        System.out.println("Full Name: " + firstPart + " " + secondPart);
    }
 
    public static void main(String[] args) {
        NamePrinter namePrinter = new NamePrinter();
        namePrinter.printFullName("Alice"); // prints "Full Name: Alice"
        namePrinter.printFullName("Bob", "Smith"); // prints "Full Name: Bob Smith"
    }
}

The advantage is that the same functionality is called with two different numbers of inputs. While invoking the method according to the input we are passing, (In this case either one string value or two string values) the corresponding method is executed.

Methods can be overloaded:

  • Based on the number of parameters passed.
  • Example: method(String s) and method(String s1, String s2).
  • Based on the order of parameters.
  • Example: method(int i, float f) and method(float f, int i)).

Note : Methods cannot be overloaded by changing just the return type (int method() is considered the same as String method() and will throw a RuntimeException if attempted). If you change the return type you must also change the parameters in order to overload.


Explaining what is method overloading and overriding

Explaining what is method overloading and overriding

Method Overloading

Method overloading (also known as static Polymorphism) is a way you can have two (or more) methods (functions) with same name in a single class. Yes its as simple as that.

public class Shape{
	//It could be a circle or rectangle or square
	private String type;
 
	//To calculate area of rectangle
	public Double area(Long length, Long breadth){
		return (Double) length * breadth;
	}
 
	//To calculate area of a circle
	public Double area(Long radius){
		return (Double) 3.14 * r * r;
	}
}

This way user can call the same method for area depending on the type of shape it has.

But the real question now is, how will java compiler will distinguish which method body is to be executed?

Well Java have made it clear that even though the method names (area() in our case) can be same but the arguments method is taking should be different.

  • Overloaded methods must have different arguments list (quantity and types).

That being said we cannot add another method to calculate area of a square like this : public Double area(Long side) because in this case, it will conflict with area method of circle and will cause ambiguity for java compiler.

Thank god, there are some relaxations while writing overloaded methods like

  • May have different return types.
  • May have different access modifiers
  • May throw different exceptions.

Why is this called static polymorphism?

Well that's because which overloaded methods is to be invoked is decided at compile time, based on the actual number of arguments and the compile-time types of the arguments.

  • One of common reasons of using method overloading is the simplicity of code it provides. For example remember String.valueOf() which takes almost any type of argument? What is written behind the scene is probably something like this:
static String valueOf(boolean b)
static String valueOf(char c)
static String valueOf(char[] data)
static String valueOf(char[] data, int offset, int count)
static String valueOf(double d)
static String valueOf(float f)
static String valueOf(int i)
static String valueOf(long l)
static String valueOf(Object obj)

Method Overriding

Well, method overriding (yes you guess it right, it is also known as dynamic polymorphism) is somewhat more interesting and complex topic.

In method overriding we overwrite the method body provided by the parent class. Got it? No? Let's go through an example.

public abstract class Shape{
	public abstract Double area(){
		return 0.0;
	}
}

So we have a class called Shape and it has method called area which will probably return the area of the shape.

Let's say now we have two classes called Circle and Rectangle.

public class Circle extends Shape {
	private Double radius = 5.0;
 
	// See this annotation @Override, it is telling that this method is from parent
	// class Shape and is overridden here
	@Override
	public Double area(){
		return 3.14 * radius * radius;
	}
}

Similarly rectangle class:

public class Rectangle extends Shape {
	private Double length = 5.0;
	private Double breadth= 10.0;
 
	// See this annotation @Override, it is telling that this method is from parent
	// class Shape and is overridden here
	@Override
	public Double area(){
		return length * breadth;
	}
}

So, now both of your children classes have updated method body provided by the parent (Shape) class. Now question is how to see the result? Well lets do it the old psvm way.

public class AreaFinder{
 
	public static void main(String[] args){
		//This will create an object of circle class
		Shape circle = new Circle();
		//This will create an object of Rectangle class
		Shape rectangle = new Rectangle();
 
		// Drumbeats ......
		//This should print 78.5
		System.out.println("Shape of circle : "+circle.area());
		//This should print 50.0
		System.out.println("Shape of rectangle: "+rectangle.area()); 
 
	}
}

Here's a chart to better compare the differences between these two:

Method Overloading Method Overriding
Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
Method overloading is performed within class. Method overriding occurs in two classes that have IS-A (inheritance) relationship.
In case of method overloading, parameter must be different. In case of method overriding, parameter must be same.
Method overloading is the example of compile time polymorphism. Method overriding is the example of run time polymorphism.
In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter. Return type must be same or covariant in method overriding.

Basic Programs