Answer (1)
akashkumarpati.2000 7th Oct, 2020

Hey Arshu,

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) );

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

VIT Bhopal University | M.Tec...
Apply
M.Tech admissions open @ VIT Bhopal University | Highest CTC 52 LPA | Apply now
Amity University | M.Tech Adm...
Apply
Ranked amongst top 3% universities globally (QS Rankings).
Amity University Noida MBA Ad...
Apply
Amongst top 3% universities globally (QS Rankings)
Graphic Era (Deemed to be Uni...
Apply
NAAC A+ Grade | Among top 100 universities of India (NIRF 2024) | 40 crore+ scholarships distributed
XAT- Xavier Aptitude Test 2026
Apply
75+ years of legacy | #1 Entrance Exam | Score accepted by 250+ BSchools | Apply now
Amity University Noida B.Tech...
Apply
Among Top 30 National Universities for Engineering (NIRF 2024) | 30+ Specializations | AI Powered Learning & State-of-the-Art Facilities
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