Results 1 to 8 of 8
Thread: Function Overloading
- 02-28-2012, 07:44 PM #1
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
Function Overloading
Hi Mentors,
I want to calculate the area of circle, triangle, rectangle, square & cube within a single program with the help of function overloading. the issue i am facing is that that in some of the area function definitions I have only one parameter.
please have a look at my code:-
Java Code:/*circle, triangle, rectangle, square, pentagon, cube, cylinder, cone, cuboid*/ class AreaShape { int area; void Area(int b, int h) //triangle { area = (b*h)/2; System.out.println("area of triangle is:-" + area); } void Area(int r) // circle { area = (int) 3.14*r*r; System.out.println("area of circle is:-" + area); } void Area(int w , int h) //rectangle { area = w*h; System.out.println("area of rectangle is:-" + area); } void Area(int s) //square { area = s*s; System.out.println("area of square is:-" + area); } void Area(int a) //cube { area = 6*a*a; System.out.println("area of square is:-" + area); } } class CalArea { public static void main(String [] args) { AreaShape obj1 = new AreaShape(); obj1.Area(4,5); obj1.Area(3); obj1.Area(1,4); obj1.Area(6); obj1.Area(7); } }
error i am getting is as below:-

I want to use only one data type either an int or a float as arguments to my methods.Last edited by ankiit; 02-28-2012 at 08:48 PM.
- 02-28-2012, 07:54 PM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Function Overloading
Java is case sensitive.int area;
void area(int r)
EDIT: This is incorrect. Variables and methods can have the same name. Perhaps not good style, but still compile-able.Last edited by Diargg; 02-28-2012 at 08:18 PM.
- 02-28-2012, 08:08 PM #3
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
- 02-28-2012, 08:16 PM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Function Overloading
Forgive me, I saw an error where there wasn't one. Turns out you can have variables with the same name as a method. Go figure.
However, for your code:
If I say "area(6)", do you know which one I'm talking about? If you don't, neither does your program. You need different parameters, switching inside the method, or different method names.
Also:
Does not constitute overloading.void Area(int b, int h)
void area(int r)
- 02-28-2012, 08:45 PM #5
- 03-01-2012, 01:22 PM #6
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
- 08-20-2012, 06:41 PM #7
Member
- Join Date
- Aug 2012
- Posts
- 1
- Rep Power
- 0
Re: Function Overloading
In order to make overloading works, you have to have unique signatures.
they are ok.Java Code:void Area(int b, int h) {...} void Area(int r) {...}
Since they use different signature you can call:
Java Code:Area(10, 20); // to execute the first function or.. Area(5); // to execute the second function
But the signature of the fallowing functions are identical, even though the variable name are different because it doesn't matter, the types are the sameand also the same forJava Code:void Area(int b, int h){...} void Area(int w, int h) {...}Here the definition is ambiguous.Java Code:void Area(int r){...} void Area(int s){...} void Area(int a){...}
- 08-20-2012, 08:43 PM #8
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Function Overloading
To add to what previous replier already pointed out, I noticed that you used 3.14 instead of Pi. To use Pi and other useful math functions, you can use the Java.Lang.Math API.
Note how I calculate the area of a circle with the radius 3, by using different Math methods.
Cheers,Java Code:public class Geometry { public static void main(String[] args) { circleArea(3); } public static void circleArea(int radius){ double area = Math.PI*Math.pow(radius, 2); System.out.println(area); } }
Z!
Similar Threads
-
Overloading or Overriding...
By chathura87 in forum New To JavaReplies: 5Last Post: 02-22-2011, 01:46 AM -
overloading
By Bhuvan in forum Advanced JavaReplies: 6Last Post: 01-02-2011, 09:15 AM -
Overloading qn
By diskhub in forum New To JavaReplies: 18Last Post: 06-07-2010, 05:19 PM -
Overloading?
By padutch2 in forum New To JavaReplies: 2Last Post: 12-31-2007, 03:26 AM -
Method/Operator Overloading
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 08:33 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks