Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By cobwer

Thread: Function Overloading

  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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:-
    Function Overloading-fo.png

    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.

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Function Overloading

    int area;
    void area(int r)
    Java is case sensitive.

    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.

  3. #3
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Function Overloading

    Quote Originally Posted by Diargg View Post
    Java is case sensitive.
    I am using function overloading, in which every function has same name.

    My point is that i want to use on integers as an argument to the functions, since area of square is a*a and for cube is 6*a*a. I have an issue with this function overloading.

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default 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:
    void Area(int b, int h)
    void area(int r)
    Does not constitute overloading.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: Function Overloading

    Quote Originally Posted by ankiit View Post
    I am using function overloading
    Java has methods. Not functions.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Function Overloading

    Quote Originally Posted by DarrylBurke View Post
    Java has methods. Not functions.

    db
    Hi,

    I agree to your point but still the technique is known as function overloading and not method overloading.

  7. #7
    cobwer is offline Member
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default Re: Function Overloading

    In order to make overloading works, you have to have unique signatures.
    Java Code:
    void Area(int b, int h) {...}
    void Area(int r) {...}
    they are ok.
    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 same
    Java Code:
    void Area(int b, int h){...}
    void Area(int w, int h) {...}
    and also the same for
    Java Code:
    void Area(int r){...}
    void Area(int s){...}
    void Area(int a){...}
    Here the definition is ambiguous.
    Zyril likes this.

  8. #8
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default 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.

    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);
          }
    }
    Cheers,
    Z!

Similar Threads

  1. Overloading or Overriding...
    By chathura87 in forum New To Java
    Replies: 5
    Last Post: 02-22-2011, 01:46 AM
  2. overloading
    By Bhuvan in forum Advanced Java
    Replies: 6
    Last Post: 01-02-2011, 09:15 AM
  3. Overloading qn
    By diskhub in forum New To Java
    Replies: 18
    Last Post: 06-07-2010, 05:19 PM
  4. Overloading?
    By padutch2 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:26 AM
  5. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •