Have you ever thought about how you can use the same method name for different tasks in Java? Or how does a child class change the behavior of its parent method? What if you want to reuse method names without writing completely new ones? And how does Java know which version of a method to run during compile-time or at runtime?
In this article, we will understand the concepts of Method Overloading and Method Overriding, the key differences between them, and when and how to use them effectively.
Table of Contents:
Method Overloading in Java
Method Overloading is a concept in object-oriented programming, in which you can create more than one method with the same name, but with different types or numbers of inputs. In this, the compiler decides which method to call, based on the arguments you pass. It is a type of polymorphism.
Method Overloading happens at compile-time, so this is also called Compile-Time Polymorphism or Static Polymorphism. Also, method overloading does not depend on inheritance.
Rules for Method Overloading:
To overload a method in Java, you must change at least one of the following:
1. All overloaded methods must have the same name but different parameter lists. For example,
public void show() {}
public void show(int a) {}
In the above example, both methods are named as show(), so they follow this rule.
2. The types of parameters of the methods should be different. For two methods with the same name, the number or types of parameters must be different. For example,
public void display(int a) {} // Method with int parameter
public void display(double a) {} // Method with double parameter
public void display(String s) {} // Method with String parameter
In the above example, all display() methods are overloaded because they take different types of parameters
3. The order of parameters should be different if the types are different. If the number of parameters in the method are same, then changing their order will also count as valid overloading. For example,
public void print(int a, String b) {}
public void print(String b, int a) {}
In the above example, the method print() has 2 parameters, a and b, the method can be overloaded if the order of the parameters is changed.
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
For example,
Output:
Explanation:
In the above Java code, the add function is used to add different types of numbers, like integers, two floats, or three integers. The program chooses the right method based on the type and number of values you pass.
Advantages of Method Overloading
- Method Overloading improves code readability by using the same method name for similar tasks.
- It also keeps the code clean by letting you use the same method name for different tasks.
- It enables compile-time polymorphism for flexible method behaviour.
- Method Overloading makes code maintenance easier by grouping related logic under one name.
Disadvantages of Method Overloading
- Method Overloading confuses developers when more than one overloaded method is present.
- It increases the code complexity due to more than one method definition.
- You cannot overload a method by just changing its return type, the parameters must be different.
- Method Overloading can cause errors if the parameters are incorrect and match the wrong method.
Method Overriding in Java
When a child class has a method with the same name and parameters as the parent class, and provides its own implementation. This is called method overriding. The method that has to be executed is chosen when the program runs, not when it is compiled, hence used in runtime polymorphism.
Rules for Method Overriding:
- The method name in the subclass must be the same, otherwise, Java treats it as a new method, not an override.
class Animal {
void sound() {
}
}
class Dog extends Animal {
void bark() {
}
}
- The method in a subclass must have the exact same parameter list as the methods that are present in its superclass.
class Animal {
void sound(String type) {
}
}
class Dog extends Animal {
void sound(String type) {
}
}
- In method overriding, the return type of the overriding method in its subclass must be exactly the same as the method in the superclass.
class Animal {
Animal getAnimal() {
return new Animal();
}
}
class Dog extends Animal {
Animal getAnimal() {
return new Dog();
}
}
For example,
Output:
Explanation:
In the above Java code, the child class Student changes the sayHello() method of the parent class Person. When sayHello() is called, the version that runs depends on the actual object, not the reference type. This helps change behaviour in the child class while keeping the same method name.
Get 100% Hike!
Master Most in Demand Skills Now!
Advantages of Method Overriding
- Method overriding allows you to change the behaviour of a method in the child class as per the needs.
- It supports runtime polymorphism, where the method runs based on the object, not the reference.
- It improves the reusability of the code and allows modifying parent class methods easily.
- Method overriding makes code simpler by using one method name for different kinds of work.
Disadvantages of Method Overriding
- Method overriding can be confusing if you are not careful with method names or signatures.
- In method overriding, debugging can be tricky, especially when many classes override the same method.
- In method overriding, the overridden method may break existing behaviour from the parent class if not used properly.
- It is slower than compile-time binding, because the method is chosen at runtime.
Difference Between Method Overloading and Method Overriding in Java
1. Polymorphism type
- Method overriding is a runtime polymorphism, because the program decides which method to run while the program is running. It occurs when the class gives the specific implementation of a method that is already defined in the parent class. In the below example, the class Dog overrides the method sound() of Animal, and at runtime, if an object of Dog is referenced using an Animal type, the overridden method will be called.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
- Method overloading is a compile-time polymorphism, because the method that is to be called is decided during the compilation, as per the method signature. In the below example, both methods add() have the same name but different parameter types; hence, the compiler decides which method to call, based on the method signature, i.e., the number and type of arguments passed.
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
2. Class Relationship
- Method overriding happens between the two classes that have a parent-child relationship, like inheritance. A subclass overrides the method that is defined in its superclass to change or extend its behaviour. In the below example, the class Car extends its parent class Vehicle.
class Vehicle {
void start() {
System.out.println("Vehicle starts");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car starts");
}
}
- Method overloading happens in the same class or a child class if methods are inherited from another class. Different methods can have the same name, but they should have different parameters. In the below example, both the print() methods are in the same class but with different parameters.
class Printer {
void print(String text) {
System.out.println(text);
} void print(int number) {
System.out.println(number);
}
}
3. Return Type
- In method overriding, the return type of the different methods has to be the same as that of the parent class. In the below example, the method type() in the class Animal and the method type() in the class Dog have the same return type.
class Animal {
String type() {
return "Animal";
}
}
class Dog extends Animal {
@Override
String type() {
return "Dog";
}
}
- In method overloading, the return types can be different, but they must differ in the parameter list. However, the return type alone cannot distinguish overloaded methods. In the below example, the method add() has 2 different return types, i.e., int and double.
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Note: In method overloading, the return type alone cannot differentiate overloaded methods.
4. Access Modifier Rule
- In method overriding, the child method should have the same or a more accessible modifier than the parent method. The
protected
method cannot be overridden as private, because it has a higher access level. In the below example, both the methods sound() have the same access modifier, public.
class Animal {
public void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
- Method overloading does not have any such type of restriction on access modifiers; they can have different access modifiers. In the example below, the method print has public, private, and protected modifiers.
class Printer {
public void print(String text) {
System.out.println(text);
}
private void print(int number) {
System.out.println(number);
}
protected void print(double value) {
System.out.println(value);
}
}
5. Static Methods
- Method overriding does not apply to static methods because static methods are resolved based on the class type, not the object type, so they are bound at compile time and do not support polymorphism.
- Method overloading can be applied to static methods as long as the parameter list is different.
6. Binding Time
- Method overriding is resolved at runtime, because the JVM determines which method is appropriate for the method implementation.
- Method overloading is resolved at compile time because the compiler resolves the method call during compilation.
7. Method Signature
- In method overriding, the signatures of the method must be the same as that of the parent method.
- In method overloading, the signatures of the methods must be different.
8. Constructor Behavior
- In method overriding, the constructors cannot be overridden, because constructors are not inherited, and hence cannot be overridden. In the below example, both class have their different constructors, Animal() and Dog().
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog constructor");
}
}
- In method overloading, the constructors can be overloaded, but in the same class. In the below example, the class Person has 3 different constructors with different parameters.
class Person {
Person() {
System.out.println("No-arg constructor");
}
Person(String name) {
System.out.println("Name: " + name);
}
Person(String name, int age) {
System.out.println("Name: " + name + ", Age: " + age);
}
}
- Method overriding is slower because the method call is resolved at runtime.
- Method overloading is faster because which method has to be called is decided at compile time.
10. Inheritance Requirement
- In method overriding, inheritance is required because without the parent class and the child class, there would be nothing to override.
- In method overloading, inheritance is not required because it can occur in the same class.
Characteristic |
Method Overriding |
Method Overloading |
Polymorphism |
Runtime |
Compile-time |
Class Relation |
Inheritance |
Same class |
Return Type |
Same/Covariant |
Can differ |
Access Modifier |
Restricted |
No restriction |
Static Methods |
Cannot be overridden |
Can be overloaded |
Binding |
Dynamic |
Static |
Signature |
Same |
Different |
Constructor |
Not allowed |
Allowed |
Performance |
Slower |
Faster |
Inheritance |
Required |
Not required |
When to Use Method Overriding in Java?
- Method Overriding is mainly used when you want a child class to change the behaviour of a method that it inherits from its parent class.
- It is also used when the basic logic is useful in a superclass, but the subclasses need their own version, along with the basic one. It helps you to follow the principles of OOPs, like runtime polymorphism, which makes the code meaningful.
- For example, the superclass can be Vehicle, and its subclasses can be bike and car.
When to Use Method Overloading in Java?
- Method Overloading is used when you want to perform similar actions in a class, but with different inputs.
- It mainly does not create multiple methods instead, it uses the same method name, but with different parameters or counts, which makes your code easier to understand.
- For example, the print() method can be used to print a string, a number, or even two strings.
Unlock Your Future in Java
Start Your Java Journey for Free Today
Conclusion
From the above article, we conclude that method overloading and method overriding make your code easier to manage. Overloading allows you to use the same method in one class, but with different inputs, which helps when you are doing the same task differently. Overriding is used when the working of the parent class needs to be changed, and the behaviour is more specific. Overloading works at compile-time, while overriding happens at runtime. Hence, a person needs to know when and how to use both programs to make their code more flexible and easier to understand.
If you want to learn more about this topic, you can refer to our Java course.
Overloading and Overriding – FAQs
Q1. What is the difference between method overriding and method overloading in Java?
Overloading means the same method name with different parameters; overriding means redefining a parent method in the child class.
Q2. Can we overload the main method?
Yes, we can overload the main method with different parameter lists.
Q3. Can a constructor be overridden in Java?
No, constructors cannot be overridden because they are not inherited.
Q4. What cannot be overridden in Java?
Static methods, constructors, and final methods cannot be overridden.
Q5. Can we inherit a constructor?
No, constructors are not inherited in Java.