- Overriding and overloading methods in java
- Overriding vs Overloading in Java
- Comparing overriding and overloading
- Overriding and overloading example
- Overriding
- Overloading
- Conclusion
- Method Overloading vs Method Overriding in Java – What’s the Difference?
- What is Method Overloading in Java?
- Key Rules of Method Overloading
- What is Method Overriding in Java?
- Key Rules of Method Overriding
- Conclusion
Overriding and overloading methods in java
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java
Overriding vs Overloading in Java
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 the method signature (name and parameters) are the same in the superclass and the child class, it’s called overriding. When two or more methods in the same class have the same name but different parameters, it’s called overloading.
Comparing overriding and overloading
Overriding | Overloading |
---|---|
Implements “runtime polymorphism” | Implements “compile time polymorphism” |
The method call is determined at runtime based on the object type | The method call is determined at compile time |
Occurs between superclass and subclass | Occurs between the methods in the same class |
Have the same signature (name and method arguments) | Have the same name, but the parameters are different |
On error, the effect will be visible at runtime | On error, it can be caught at compile time |
Overriding and overloading example
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); > >
Overriding
The process() method and int i, int j parameters in Processor are overridden in the child class MathProcessor . Line 7 and line 23:
public class Processor public void process(int i, int j) /* . */ > > /* . */ class MathProcessor extends Processor @Override public void process(int i, int j) /* . */ > >
And process() method and int[] ints in Processor are also overridden in the child class. Line 11 and line 28:
public class Processor public void process(int[] ints) /* . */ > > /* . */ class MathProcessor extends Processor @Override public void process(Object[] objs) /* . */ > >
Overloading
The process() method is overloaded in the Processor class. Lines 7, 11, and 15:
public class Processor public void process(int i, int j) /* . */ > public void process(int[] ints) /* . */ > public void process(Object[] objs) /* . */ > >
Conclusion
In this article, we covered overriding and overloading in Java. Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Method Overloading vs Method Overriding in Java – What’s the Difference?
Mikael Lassa
In Java, method overloading and method overriding both refer to creating different methods that share the same name.
While the two concepts share some similarities, they are distinct notions with markedly different use cases. Having a firm grasp of them is important in building strong foundational Java skills.
In this post, we’ll explore the key rules of method overloading and overriding, as well as the differences between them.
What is Method Overloading in Java?
Overloading a method, in simple terms, means creating a different method with the same name in the same class, but with a different parameter list.
There can be many cases where you might need to handle different types of input for the same operation, and method overloading is one way to handle such cases.
For example, let’s say you want to create a method that performs an addition of two numbers. This calculation is designed to return a number as its output. If your method handles parameters of type int , attempting to call it by passing values of type double as arguments results in a compilation error.
For this reason, you might want to overload the method by creating a new version of that method that is able to handle a different type of input (in this case of type double ):
public class Calculator < public int sum(int a, int b) < return a + b; >public double sum(double a, double b) < return a + b; >>
In the example above, the sum() method is overloaded, because it is defined more than once within the same class, but with a different parameter list.
A method can also be overloaded by changing the number of parameters. On this basis, the following methods are also legal examples of how the sum() method can be overloaded, assuming they are placed within the same class:
public int sum(int a, int b, int c) < return a + b + c; >protected void sum()
Note that, as in some of the examples above, you can also change the return type or the access modifier, but this is not mandatory.
Key Rules of Method Overloading
Remember these rules when overloading a method:
- The overloaded and overloading methods must be in the same class (Note: this includes any methods inherited, even implicitly, from a superclass).
- The method parameters must change: either the number or the type of parameters must be different in the two methods.
- The return type can be freely modified.
- The access modifier ( public , private , and so on) can be freely modified.
- Thrown exceptions, if any, can be freely modified.
What is Method Overriding in Java?
Method overriding refers to redefining a method in a subclass that already exists in the superclass.
When you call an overridden method using an object of the subclass type, Java uses the method’s implementation in the subclass rather than the one in the superclass. For this reason, an understanding of the concept of inheritance in Java is important in order to get a good grasp of method overriding.
Any subclass can generally override any method from a superclass, unless a method is marked with the final or static keywords. The overriding method must not change the name and parameter list of the overridden method.
While not compulsory, it is good practice to use the @Override annotation when overriding a method: this annotation will check that the method is being overridden correctly, and will warn you if that’s not the case.
In the following example, you’ll see a class Car that extends the class Vehicle . The Car class overrides the move() method from the superclass, and this is made explicit by the use of the @Override annotation. The two methods are implemented differently in the method body.
class Vehicle < public void move() < System.out.println("The vehicle is moving"); >> class Car extends Vehicle < @Override public void move() < System.out.println("The car is moving"); >>
The choice of which version of move() will be called is based on the object type the method is being called on. Note that the version of the overridden method that is called is determined at runtime and is based on the object type, not the object reference.
This is illustrated in the following example, particularly in the third call to move() : while the method is called on an object reference on type Vehicle , the actual object is of type Car . The type of the object here is determined at runtime, and the version of the method that is called is therefore the one from the Car subclass.
public static void main(String[] args) < Vehicle vehicle = new Vehicle(); vehicle.move(); // Prints: The vehicle is moving Car car = new Car(); car.move(); // Prints: The car is moving Vehicle secondVehicle = new Car(); secondVehicle.move(); // Prints: The car is moving >
Key Rules of Method Overriding
Remember these rules when overriding a method:
- The parameter list must not change: the overriding method must take the same number and type of parameters as the overridden method – otherwise, you would just be overloading the method.
- The return type must not change (Note: if the method returns an object, a subclass of that object is allowed as the return type).
- The access modifier must be either the same or a less restrictive one (for example, if the overridden method is protected , you can declare the overriding method as public , but not private ).
- Thrown checked exceptions, if any, can be removed or reduced by the overriding method. This means that the overriding method can throw the same checked exception as the overridden method, or a subclass of that checked exception, but not a broader exception. This restriction does not apply to unchecked exceptions.
Conclusion
In this article we explored the main rules of method overloading and method overriding in Java. You saw that the main point of overloading a method is to change its parameter list in order to implement a different behaviour based on the arguments that are passed to it.
Overriding, on the other hand, refers to re-defining the same method, with the same parameter list, in a subclass in order to tailor its behaviour to the needs of the subclass.
These concepts are interlinked with some of the core object-oriented programming ideas, such as inheritance and polymorphism, so they’re fundamental in order to master Java. They can cause some confusion, especially for beginners, but having a firm understanding of the rules and the uses of these concepts should help developers write more efficient and readable code.
If you would like to read any of my other articles, you are welcome to check out my blog.