Assignments/Article By Gowsika Yogaraj

Overloading & Overriding in Java

Student Assignment

Overloading and overriding in java

Published on Nov 06, 2022

Overloading

In Java, two or more methods have the same name but different types of parameters, a different number of parameters, or both. These methods are called overloaded methods and called method overloading.

For Example

void add (int a, int b) { }

void add (int a, float b) {}

double add (float a, double b) { }

int add(int a, int b, int c) { }

Here the return types of the add () function are different. Because overloading is not associated with the return types, overloaded methods may have the same or different return types, but they must differ in the parameters.

Example of Method Overloading

package uk.lset.overloading;

public class MethodOverloading {

public void add(int a,int b)

{

System.out.println(“The Addition of two numbers is :” +(a+b));

}

public void add(int a,int b,int c)

{

System.out.println(“The Addition of three numbers is :” + (a+b+c));

}

public void add(float a,int b)

{

System.out.println(“The Addition of two double numbers is :” + (a+b));

}

public void add(double a,int b)

{

System.out.println(“The Addition of two double numbers is :” + (a+b));

}

public static void main(String[] args) {

MethodOverloading obj=new MethodOverloading();

obj.add(10.0,20);

obj.add(11.5, 20);

obj.add(50,70);

obj.add(20,30,70);

}

}

The method has Five Components, they are

  • Access Modifier
  • Access Modifier
  • The method Name
  • The parameter list in the ()
  • The method body is enclosed between the braces

Overriding

package uk.lset.override;

public class Animal {

public void disply()

{

System.out.println(“I am an animal”);

}

}

package uk.lset.override;

public class Dog extends Animal {

public void display()

{

super.disply();

System.out.println(“I am dog i can bark”);

}

}

package uk.lset.override;

public class Main {

public static void main(String[] args) {

Dog obj=new Dog();

obj.display();

}

}

Super Keyword in Overriding

The super keyword in overriding helps us to access the method of superclass from the subclass after overriding.

The differences between Method Overloading and Method Overriding in Java are as follows:

Method Overloading Method Overriding
Method overloading is a compile-time polymorphism. Method overriding is a run-time polymorphism.
It helps to increase the readability of the program. It is used to grant the specific implementation of the method which is already provided by its parent class or superclass.
It occurs within the class. It is performed in two classes with inheritance relationships.
Method overloading may or may not require inheritance. Method overriding always needs inheritance.
In method overloading, methods must have the same name and different signatures. In method overriding, methods must have the same name and same signature.
In method overloading, the return type can or cannot be the same, but we must change the parameter. In method overriding, the return type must be the same or co-variant.
Static binding is being used for overloaded methods. Dynamic binding is being used for overriding methods.
Poor Performance due to compile time polymorphism. It gives better performance. The reason behind this is that the binding of overridden methods is being done at runtime.
Private and final methods can be overloaded. Private and final methods can’t be overridden.
The argument list should be different while doing method overloading. The argument list should be the same in method overriding.

Student Assignment

The views expressed in this document are those of the author and do not necessarily reflect the position of the London School of Emerging Technology. View the detailed policy Disclaimer for Student and Personal Websites

Our Latest Blog

Why Learning Data Science with Python Is the Smartest Career Move Today

Why Learning Data Science with Python Is the Smartest Career Move Today

In today’s rapidly changing world, technology and data play a central role in nearly every...
Read More
Learn, Build, and Innovate_ Machine Learning with Python at London’s Tech Institute

Learn, Build, and Innovate: Machine Learning with Python at London’s Tech Institute

In recent years, machine learning has become an essential part of many fields, from healthcare...
Read More
Accelerate Your Tech Career with LSET Bootcamps in Java, Python, DevOps & More

Accelerate Your Tech Career with LSET Bootcamps in Java, Python, DevOps & More

In today’s fast-changing technology landscape, staying competitive in the job market requires more than just...
Read More

Upcoming Workshop

International Workshop on Emerging AI & Machine Learning Innovation

  • Explore
  • Learn
  • Innovate

Join global tech minds at LSET for a hands-on journey into AI & Machine Learning Innovation.

Limited Seats Sign Up Today!

  • Certificates
  • Live Projects
  • Networking

This will close in 0 seconds