Function Overloading in Java

Hello Friends,
Functions overloading is a concept of Object Oriented Programming System. This concept states that a function can have more than one definition but each time with different return type or parameter variables. Every time it can perform different task. In my case it performs the calculation to find the area according to function called i.e. if the function is called by passing one parameter in int type it returns the area of square but if called with one parameter but in double type it returns the area of circle and so on …..
Java Code for function overloading program :-

import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Virus
 */
public class functionOverloading {
    public static void main(String [] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("\tMENU\n1.Square\n2.Rectangle\n3.Triangle\n4.Circle\nEnter Your choice 1/2/3/4 =>");
        int choice=sc.nextInt();
        functionOverloading over=new functionOverloading();
        switch(choice)
        {
            case 1:
            {
                System.out.print("Enter the side of square : ");
                int s=sc.nextInt();
                int area=over.area(s);//calling single parameter funtion with integer return type
                System.err.println("Area of square with side "+s+" is "+area);
                break;
            }
            case 2:
            {
                System.out.print("Enter the length of rectangle : ");
                double l=sc.nextDouble();
                System.out.print("Enter the breadth of rectangle : ");
                double b=sc.nextDouble();
                double area=over.area(l,b);//calling two parameter funtion with double return type
                System.err.println("Area of recangle with length "+l+" and breadth "+b+" is "+over.area(l,b));
                break;
            }
            case 3:
            {
                System.out.print("Enter the side1 of triangle : ");
                double s1=sc.nextDouble();
                System.out.print("Enter the side2 of triangle : ");
                double s2=sc.nextDouble();
                System.out.print("Enter the side3 of triangle : ");
                double s3=sc.nextDouble();
                double area=over.area(s1,s2,s3);//calling three parameter funtion with double return type
                System.err.println("Area of triangle with sides "+s1+", "+s2+" and "+s3+" is "+over.area(s1,s2,s3));
                break;
            }
            case 4:
            {
                System.out.print("Enter the radius of circle : ");
                double r=sc.nextDouble();
                double area=over.area(r);//calling single parameter funtion with double return type
                System.err.println("Area of circle with radius "+r+" is "+over.area(r));
                break;
            }
            default:
                System.out.println("Wrong Choice !!!!");
        }
    }
    //This overloaded functions returns the area of square
    public int area(int side)
    {
        return side*side;
    }
    
    //This overloaded functions returns the area of rectangle
    public double area(double length, double breadth)
    {
        return length*breadth;
    }
    
    //This overloaded functions returns the area of triangle
    public double area (double side1, double side2, double side3)
    {
        double s=(side1+side2+side3)/2;
        return Math.sqrt((s*(s-side1)*(s-side2)*(s-side3)));
    }
    
    //This overloaded functions returns the area of Circle
    public double area(double radius)
    {
        return (22/7)*(radius*radius);
    }
}

Output Screenshots:-

 

Leave a Reply