0% found this document useful (0 votes)
2 views

Overloading in Java

Method overloading in Java allows multiple methods with the same name but different parameter lists, enhancing code readability and flexibility. Constructor overloading similarly permits multiple constructors with the same name, providing various ways to initialize objects. Both concepts exemplify compile-time polymorphism, where the method to be executed is determined during compilation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Overloading in Java

Method overloading in Java allows multiple methods with the same name but different parameter lists, enhancing code readability and flexibility. Constructor overloading similarly permits multiple constructors with the same name, providing various ways to initialize objects. Both concepts exemplify compile-time polymorphism, where the method to be executed is determined during compilation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Method Overloading in Java

Method overloading allows multiple methods in a class to have the same name but with different
parameter lists. It provides flexibility to perform a similar operation in different ways depending on
the arguments passed.

Characteristics of Method Overloading:

1. Same Name: All methods have the same name.

2. Different Parameter Lists: Differ in:

o Number of parameters.

o Type of parameters.

o Order of parameters.

3. Compile-Time Polymorphism: The compiler determines which method to invoke based on


the method signature during compilation.

4. Return Type: Return type alone cannot distinguish overloaded methods.

Benefits of Method Overloading in This Example:

1. Code Readability: The use of the same method name, area, makes the code more intuitive.

2. Flexibility: The same class handles the calculation of areas for multiple shapes, avoiding the
need for separate classes or methods with different names.

3. Compile-Time Polymorphism: The method to be executed is determined at compile-time,


ensuring efficiency.

Constructor Overloading in Java

Constructor overloading allows a class to have multiple constructors with the same name (the class
name) but with different parameter lists. It provides flexibility to initialize objects in different ways
depending on the parameters passed.

Characteristics of Constructor Overloading:

1. Same Name: All constructors have the same name as the class.

2. Different Parameter Lists: Differ in:

o Number of parameters.

o Type of parameters.

o Order of parameters.
3. Default Constructor: If no constructor is explicitly defined, Java provides a default
constructor. When constructors are explicitly defined, the default constructor must be added
explicitly if needed.

class Areacalculate {

// Method to calculate the area of a square

double area(double side) {

return side * side; // side^2

// Overloaded method to calculate the area of a rectangle

double area(double length, double breadth) {

return length * breadth; // length * breadth

// Overloaded method to calculate the area of a circle

double area(double radius, boolean isCircle) {

return Math.PI * radius * radius; // π * r^2

public class Main {

public static void main(String[] args) {

AreaCalculator calculator = new AreaCalculator();

// Calculate the area of a square

System.out.println("Area of Square: " + calculator.area(5)); // side = 5

// Calculate the area of a rectangle

System.out.println("Area of Rectangle: " + calculator.area(5, 10)); // length = 5, breadth = 10


// Calculate the area of a circle

System.out.println("Area of Circle: " + calculator.area(7, true)); // radius = 7

You might also like