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

Java Spring Boot

Java Spring Boot: A Comprehensive Introduction to Microservices

Introduction to Java Spring Boot and Microservices Java Spring Boot has emerged as an important...
Read More
Fundamentals of Scala Programming

Mastering the Fundamentals of Scala Programming: A Complete Guide to Functional Programming

Introduction to Scala Programming Scala programming has gained immense popularity in recent years thanks to...
Read More
Java Programming

Java Basics for Beginners: A Step-by-Step Guide to Mastering Java Programming

Introduction to Java programming Java, a versatile and extensively utilised programming language, is popular among...
Read More