While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Overriding and Overloading are the core concepts in Java programming. They are the ways to implement polymorphism in our java programs. Polymorphism is one of the OOPS Concepts.
When two or more methods in the same class have the same name but different parameters, it’s called Overloading. When the method signature (name and parameters) are the same in the superclass and the child class, it’s called Overriding.
Here is an example of overloading and overriding in a Java program.
package com.journaldev.examples;
import java.util.Arrays;
public class Processor {
public void process(int i, int j) {
System.out.printf("Processing two integers:%d, %d", i, j);
}
public void process(int[] ints) {
System.out.println("Adding integer array:" + Arrays.toString(ints));
}
public void process(Object[] objs) {
System.out.println("Adding integer array:" + Arrays.toString(objs));
}
}
class MathProcessor extends Processor {
@Override
public void process(int i, int j) {
System.out.println("Sum of integers is " + (i + j));
}
@Override
public void process(int[] ints) {
int sum = 0;
for (int i : ints) {
sum += i;
}
System.out.println("Sum of integer array elements is " + sum);
}
}
The process() method is overloaded in the Processor class. Then, they are overridden in the child class MathProcessor.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
Sign up
Thanks you. because l have irritating this concept understanding after that your implement concept and explain easy understanding ,so iam very happy
- ambika
Its good but with an Example its better for more Understanding. Overall efforts are good.
- Shriganesh Mane
Great explanation of the topic. You made it simple and easy to understanding. Thank you.
- David
Nice, concise explanations. Referring my students here for this topic.
- Mark Miller
thanks for clear explanation of difference
- MOHAMMAD AHTISHAM
hello, very happy with this website.
- ranjit vamadevan