Week 107 — What is the difference between overloading and overriding (in Java)?

Question of the Week #107
What is the difference between overloading and overriding (in Java)?
16 Replies
CrimsonDragon1
Overloading is the process of defining multiple methods with the same name but different parameters in the same class:
public void someMethod(){
System.out.println("overload 1 with no parameters");
}

public void someMethod(int i){
System.out.println("overload 2 with int parameter");
}

public void someMethod(String s){
System.out.println("overload 3 with String parameter");
}

public void someMethod(String s){
System.out.println("overload 4 with object parameter");
}
public void someMethod(){
System.out.println("overload 1 with no parameters");
}

public void someMethod(int i){
System.out.println("overload 2 with int parameter");
}

public void someMethod(String s){
System.out.println("overload 3 with String parameter");
}

public void someMethod(String s){
System.out.println("overload 4 with object parameter");
}
someMethod();//overload 1 with no parameters
someMethod(1337);//overload 2 with int parameter
someMethod("Hello World");//overload 3 with String parameter
Object stringAsObject = "Hello World";
someMethod(stringAsObject);//overload 4 with object parameter
someMethod();//overload 1 with no parameters
someMethod(1337);//overload 2 with int parameter
someMethod("Hello World");//overload 3 with String parameter
Object stringAsObject = "Hello World";
someMethod(stringAsObject);//overload 4 with object parameter
When an overloaded method is called, the target method to call is determined statically and is not subject to inheritance. On the other hand, overriding is a subclass providing a different implementation of a method also present in the superclass. When calling that method on an instance of the subclass that is overridden by the subclass, the overridden method will be called:
public class SuperClass{
public void someMethod(){
System.out.println("SuperClass method called");
}
}
public class SubClass extends SuperClass{
@Override
public void someMethod(){
System.out.println("overridden method called");
}
}
public class SuperClass{
public void someMethod(){
System.out.println("SuperClass method called");
}
}
public class SubClass extends SuperClass{
@Override
public void someMethod(){
System.out.println("overridden method called");
}
}
SuperClass subClassInstanceWithSuperClassStaticType = new SubClass();
subClassInstanceWithSuperClassStaticType.someMethod();//overridden method called

SuperClass superClassInstance = new SuperClass();
superClassInstance.someMethod();//SuperClass method called

SubClass subClassInstance = new SubClass();
subClassInstance.someMethod();//overridden method called
SuperClass subClassInstanceWithSuperClassStaticType = new SubClass();
subClassInstanceWithSuperClassStaticType.someMethod();//overridden method called

SuperClass superClassInstance = new SuperClass();
superClassInstance.someMethod();//SuperClass method called

SubClass subClassInstance = new SubClass();
subClassInstance.someMethod();//overridden method called
Overriding is determined dynamically when the method is called depending on the class of the instance the method is called on (the receiver object). The annotation @Override can be used to ensure a method is actually overriding a method in a supertype. If the annotation is used on a method that isn't overriding another (for example because of a typo), there will be a compilation error.
CrimsonDragon1
Since overloading is determined statically, it only takes the static type (the type of the variable) into account when determining which signature to use for the call. If a method is overloaded in a subclass but that subclass is not used as the type of the variable, the overload will not be taken into account.
public class SuperClass{
public void someMethod(Object o){
System.out.println("SuperClass method with Object param called");
}
}
public class SubClass extends SuperClass{
@Override
public void someMethod(Object o){
System.out.println("overridden method with object param called");
}
//@Override//compiler error because this is not overridden
public void someMethod(String s){
System.out.println("overloaded SubClass method with String param called");
}
}
public class SuperClass{
public void someMethod(Object o){
System.out.println("SuperClass method with Object param called");
}
}
public class SubClass extends SuperClass{
@Override
public void someMethod(Object o){
System.out.println("overridden method with object param called");
}
//@Override//compiler error because this is not overridden
public void someMethod(String s){
System.out.println("overloaded SubClass method with String param called");
}
}
SuperClass subClassInstanceWithSuperClassStaticType = new SubClass();
subClassInstanceWithSuperClassStaticType.someMethod("Hello World");//overridden method with object param called

SubClass subClassInstance = new SubClass();
subClassInstance.someMethod("Hello World");//overloaded SubClass method with String param called
SuperClass subClassInstanceWithSuperClassStaticType = new SubClass();
subClassInstanceWithSuperClassStaticType.someMethod("Hello World");//overridden method with object param called

SubClass subClassInstance = new SubClass();
subClassInstance.someMethod("Hello World");//overloaded SubClass method with String param called
📖 Sample answer from dan1st
CrimsonDragon1
overloading is having the same method names but different parameters and overriding is giving an implementation to an abstract classes method
Submission from xodosu
CrimsonDragon1
Overloading is when you have a method with the same name with with different parameters. The signatures are different. With overriding, it means you are taking a method from a super class & implementing it differently from the super class.
Submission from inalelub
CrimsonDragon1
Overriding is redefining a method in a sub class Overloading is creating a method with the same name, but with different parameters/arguments Override example:
public abstract class Cat{

public abstract void speak();
}

public class Calico{

@Override
public void speak(){
System.out.print("meow");
}
}
public abstract class Cat{

public abstract void speak();
}

public class Calico{

@Override
public void speak(){
System.out.print("meow");
}
}
CrimsonDragon1
Overloading example:
public class Addition {
public int add(int a, int b) {
//method implementation
}

public double add(double a, double b) {
//method implementation
}
}
public class Addition {
public int add(int a, int b) {
//method implementation
}

public double add(double a, double b) {
//method implementation
}
}
Submission from oxy.desu
CrimsonDragon1
Overloading - Have same function name but with different argument. In the same class. Overriding- Have same function name, like parent class and child class. What we call in Main class(by creating object) it calls that method. Which mostly happens in inheritance.
Submission from mownika_
CrimsonDragon1
Overloading is where you have two methods whoms bytecode signature differs only in parameters, whereas overriding is where you @Override a method from a parent class, by defining a method with the same bytecode signature
CrimsonDragon1
For example, Overloading example:
public class Overload {
public static Overload newInstance() {
return newInstance(0);
}

public static Overload newInstance(int flags) { // overloaded method
...
}
}
public class Overload {
public static Overload newInstance() {
return newInstance(0);
}

public static Overload newInstance(int flags) { // overloaded method
...
}
}
Overriding example:
public class Parent {
public void doSomething() {
System.out.println("hi");
}
}

public class Child {
@Override
public void doSomething() {
System.out.println("bye");
}
}
public class Parent {
public void doSomething() {
System.out.println("hi");
}
}

public class Child {
@Override
public void doSomething() {
System.out.println("bye");
}
}
Submission from sire3
CrimsonDragon1
Overloading is the process where you create a new method with the same exact name, but with a different set of input parameters
public double e() {
return 2.71;
}
public double e(double pow) {
return Math.pow(2.71, pow);
}
public double e() {
return 2.71;
}
public double e(double pow) {
return Math.pow(2.71, pow);
}
Overriding is the process where you create a new implementation for the same method (or the first implementation for an abstract method), inside a class that you have extended from the parent class that has the original method
public interface User {
public String getRole();
}
public interface User {
public String getRole();
}
public class Moderator implements User {
@Override
public String getRole() {
return "Moderator.";
}
}
public class Moderator implements User {
@Override
public String getRole() {
return "Moderator.";
}
}
Submission from nxcnibiru
CrimsonDragon1
Method overloading: would be like same method but with different parameters e.g a math class you can have an addition method that takes in integer, double, etc.. Method overriding: is when you inherit from a class and your subclass has the same method from the superclass
Submission from lollidite
CrimsonDragon1
When you overload a function you basicly create another methode with the same name but different parameters. example:
public void doSomething(String s, int i) {}
public void doSomething(String s, long l) {}
public void doSomething(String s, int i) {}
public void doSomething(String s, long l) {}
When you overrite a function you change/write the code from a extended/implemented function. example:
public class Command1 implements Command {
@Override
public void onCommand(String s) {
doSomething(s);
}
}

public class Command2 implements Command {
@Override
public void onCommand(String s) {
doSomethingElse(s);
}
}
public class Command1 implements Command {
@Override
public void onCommand(String s) {
doSomething(s);
}
}

public class Command2 implements Command {
@Override
public void onCommand(String s) {
doSomethingElse(s);
}
}
Submission from ufo.dev
CrimsonDragon1
Overloading - methods with same signature but only difference it the number and type of parameters they have, Ex. public int calc(int a,int b){ return a + b; } public double calc(double a, double b){ return a + b; } public float calc(float a){ return a+a } They both will return sum of two numbers but their type is different and java in compile time will have that in mind. Overriding It's possible when we have class hierarcy - one class is inherited from others. Example: class Person { public void showInfo() { System.out.println("I am just a person"); } } class Student extends Person {} class RunApp{ Person p = new Person(); p.showInfo();//I am just a person Person student = new Student(); student.showInfo()//I am just a person } If child class(Student) doesn't impelement it's own version on parent's method - always it will return what is from parent's method class Person { public void showInfo() { System.out.println("I am just a person"); } } class Student extends Person { @Override //usually it work with anotation public void showInfo() { System.out.println("I am just a student"); } } class RunApp{ Person p = new Person(); p.showInfo();//I am just a person Person student = new Student(); student.showInfo()//I am just a student } In that case if implementation is of Student type it will return the children's context. Overriding depends of implementation when we make instance of an object. Person p ; //type - Person p = new Student (); // person implemented as a Student
Submission from sett9824
CrimsonDragon1
Overloading & overriding are concepts that apply to types (classes, interfaces, records, and enums). Despite the similar names, they are quite different. Overriding is only applicable to classes, and happens when a child class defines a method with the same signature^ as a method defined in a parent or grandparent class. For instance:
public class A {
public Number getTheNumber(String key) {
System.out.println("In A");
return Double.valueOf(24);
}
}

public class B extends A {
public Integer getTheNumber(String key) {
System.out.println("In B");
return Integer.valueOf(42);
}
}
public class A {
public Number getTheNumber(String key) {
System.out.println("In A");
return Double.valueOf(24);
}
}

public class B extends A {
public Integer getTheNumber(String key) {
System.out.println("In B");
return Integer.valueOf(42);
}
}
The method B::getTheNumber overrides the method A::getTheNumber. Because method dispatch occurs dynamically, the overridden implementation will be called if the actual type of the receiver is B:
A a = new B();
Number num = a.getTheNumber("a key"); // Will print "In B"
System.out.println(num); // Will print "42"
A a = new B();
Number num = a.getTheNumber("a key"); // Will print "In B"
System.out.println(num); // Will print "42"
It is also possible for the subclass' method to call the superclass' using the super keyword, e.g. super.getTheNumber(key). ^The subclass' method can also declare that it returns a subtype of the type returned by the superclass' method, as demonstrated in the example above.
CrimsonDragon1
On the other hand, overloading is when a type contains multiple methods of the same name, but with different parameters.^ For instance:
public class A {
public Number getTheNumber(String key) {
// Some logic
}
public Number getTheNumber(String key, Pattern matcher) {
// Some different logic
}
}
public class A {
public Number getTheNumber(String key) {
// Some logic
}
public Number getTheNumber(String key, Pattern matcher) {
// Some different logic
}
}
^Note that overloaded methods can have different return types, but cannot differ only by return type. The actual number and/or types of parameters must be different.
⭐ Submission from dangerously_casual
CrimsonDragon1
Overriding Means redefining a method (behavior of an object) from a parent class or from an interface. The method name and arguments must be identical. It enables polymorphism. Example: an interface called Animal with an abstract method move() and 2 concrete classes Snake and Fish implementing Animal like this :
interface Animal {
void move();
}
class Snake implements Animal {
@Override
void move() {
print("crawl on sand");
}
}
class Fish implements Animal {
@Override
void move() {
print("Swim in water");
}
}
interface Animal {
void move();
}
class Snake implements Animal {
@Override
void move() {
print("crawl on sand");
}
}
class Fish implements Animal {
@Override
void move() {
print("Swim in water");
}
}
👉 in this example the move() is redefined and controlled with the @Override annotation, and has same signature across both classes, but doesn't have same content. Overloading Means defining multiple methods with the same name but different with different argument types in the same class. It enhances code readability and usability by allowing various scenarios. Example : a class called ShopSaler with a method sell(itemName), and a second one sell(itemName, count). Which means, a ShopSaler object can do sell(itemName), but can also do sell(itemName, count) : Example :
class ShopSaler {
public void sell(String itemName) {
print("Selling " + itemName);
}
public void sell(String itemName, int count) {
print("Selling " + count + " units of " + itemName);
}
}
class ShopSaler {
public void sell(String itemName) {
print("Selling " + itemName);
}
public void sell(String itemName, int count) {
print("Selling " + count + " units of " + itemName);
}
}
⭐ Submission from firasrg5942

Did you find this page helpful?