What is overloading in Oops?

In Object-Oriented Programming (OOP), overloading is a concept where multiple methods or constructors can have the same name, but they differ in their parameters (either in type, number, or both). Overloading enhances the flexibility of your code by allowing you to use the same method name to perform different tasks, depending on the arguments passed to it.

Types of Overloading

There are two main types of overloading in OOP:

  1. Method Overloading
  2. Constructor Overloading

1. Method Overloading

Method overloading occurs when multiple methods in the same class share the same name but have different parameter lists (i.e., they accept a different number or types of arguments). The return type can be the same or different, but it is the method signature (name and parameters) that determines how the method is selected.

Example of Method Overloading

class Calculator { // Method for adding two integers public int add(int a, int b) { return a + b; } // Overloaded method for adding three integers public int add(int a, int b, int c) { return a + b + c; } // Overloaded method for adding two double numbers public double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 3)); // Calls the method with 2 integers System.out.println(calc.add(1, 2, 3)); // Calls the method with 3 integers System.out.println(calc.add(1.5, 2.3)); // Calls the method with 2 doubles } }

In this example:

  • The add method is overloaded with different parameter types and counts.
  • The compiler knows which version of the method to invoke based on the arguments passed.

2. Constructor Overloading

Constructor overloading allows you to create multiple constructors in the same class, each with a different parameter list. This helps in creating objects in different ways by providing different sets of initial values.

Example of Constructor Overloading

class Book { private String title; private String author; // Constructor with no parameters public Book() { this.title = "Unknown Title"; this.author = "Unknown Author"; } // Constructor with parameters public Book(String title, String author) { this.title = title; this.author = author; } public void displayInfo() { System.out.println("Title: " + title + ", Author: " + author); } } public class Main { public static void main(String[] args) { // Using the constructor with no parameters Book book1 = new Book(); book1.displayInfo(); // Outputs: Title: Unknown Title, Author: Unknown Author // Using the constructor with parameters Book book2 = new Book("1984", "George Orwell"); book2.displayInfo(); // Outputs: Title: 1984, Author: George Orwell } }

In this example:

  • The Book class has two constructors: one with no parameters (the default constructor) and one that accepts parameters for setting title and author.
  • This allows for flexibility when creating Book objects, as you can either use default values or specify values when constructing an object.

Benefits of Overloading

  • Flexibility: Overloading allows you to use the same method name to handle different types of input, reducing the need for multiple method names.
  • Code Clarity: Instead of using different names for methods that perform similar tasks, you can use overloading to keep the code clean and easier to understand.
  • Maintainability: Overloading makes the code easier to maintain, as changes are localized to a single method name rather than across multiple method names.

To deepen your understanding of method overloading and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:

These courses provide comprehensive insights and practical examples to help you master Object-Oriented Programming principles and excel in your technical interviews.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How to pass Google intern interview?
Analyzing resource allocation in large-scale distributed systems
What is the main focus of Tesla?
What is the difference between coding and programming?
How can I improve my engineering CV?
Performance tuning lessons for advanced coding interviews
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.