249 Views

describe the method overloading concept in java


Knhklohvfg 10th Nov, 2020
Answers (2)
Ishaan Chaudhary 19th Jul, 2022

Java enables a class to have several methods with the same name, but with distinct arguments, using method overloading.

Method overloading is supported in Java through two mechanisms:

In order to alter the number of parameters, one may

This may be done by altering parameter data type Excessive parameters cause overburdening Increasing the amount of arguments in a method might cause it to become overloaded.

akashkumarpati.2000 10th Nov, 2020

Dear Aspirant,

Regarding your query now I'm going to explain about the Method Overloading (or Function Overloading ) concept in Java.

Basically system finds the best match of the function arguments and the parameter list (i.e types and numbers or parameters) during program compilation. This phenomenon is termed as static building.

Hence function overloading is the process of defining functions/ methods with the same function names but with different numbers and types of parameters.

Reason for using it :

An object contains State and Behaviour ( Data and Functions). Sometimes it's intended to perform similar operations with a slight change in the parameters.

A number or methods are used to implement a single interface in Java i.e. polymorphism. If different function names are used for similar operations then users may have a lot of interactions and they require to remember a number of function names at the time of invocations.

Hence it's reliable to use many functions with the same namea forr similar operations.

Let's discuss the whole thing practically with a short and simple program :

Q: Write a class with the name 'volume' . Using function overloading compute the volume of a cube, a sphere and a cuboid in Java :

Answer:

import java.util.*;

class volume

{

void vl(int s)

{

int v;

v=s*s*s;

System.out.println("volume of cube:"+v);

}

void vl(double r)

{

double v;

v=1.33*3.14*r*r*r;

System.out.println("volume of sphere:"+v);

}

void vl(int l,int b,int h)

{

int v;

v=l*b*h;

System.out.println("volume of cuboid:"+v);

}

public static void main(String args[])

{

Scanner sc=new Scanner( System.in (http://System.in) ( http://System.in ) );

volume obj= new volume();

int s;

double r;

int l,b,h;

System.out.println("enter the side for cube:");

s= sc.nextInt();

System.out.println("enter the radius for sphere:");

r= sc.nextDouble();

System.out.println("enter the length for cuboid:");

l= sc.nextInt();

System.out.println("enter the bredth for cuboid:");

b= sc.nextInt();

System.out.println("enter the height for cuboid:");

h= sc.nextInt();

obj.vl(s);

obj.vl(r);

obj.vl(l,b,h);

}

}

Just go through and analyse the above code minutely . Here I'm performing the similar operation in each case ( i.e. calculating the volume) with just one class named "volume". Hence this is the method overloading .

I think now the whole thing becomes clear to you. If you've any query regarding this just ask in the comment section, I'll definitely explain.

Thank you.

Related Questions

UPES Integrated LLB Admission...
Apply
Ranked #28 amongst Institutions in India by NIRF | Ranked #1 in India for Academic Reputation by QS University Rankings | 16.6 LPA Highest CTC
SLAT 2025 - The Symbiosis Law...
Apply
Conducted by Symbiosis International (Deemed University) | Ranked #5 in Law by NIRF | Ranked #2 among best Pvt Universities by QS World Rankings
Jindal Global Law School Admi...
Apply
Ranked #1 Law School in India & South Asia by QS- World University Rankings | Merit cum means scholarships
Symbiosis Law School Pune Adm...
Apply
NAAC A++ Accredited | Ranked #5 by NIRF
Nirma University Law Admissio...
Apply
Grade 'A+' accredited by NAAC
Great Lakes PGPM & PGDM 2025
Apply
Admissions Open | Globally Recognized by AACSB (US) & AMBA (UK) | 17.3 LPA Avg. CTC for PGPM 2024 | Application Deadline: 1st Dec 2024
View All Application Forms

Download the Careers360 App on your Android phone

Regular exam updates, QnA, Predictors, College Applications & E-books now on your Mobile

150M+ Students
30,000+ Colleges
500+ Exams
1500+ E-books