Java Program to Find Area of Square Using Method Overloading - GeeksforGeeks

04 Dec, 2020
A square is a simple flat shape in a plane, defined by four points at the four corners. It has four sides with equal length and four corners with right angles.
Method Overloading: Method overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters, or both. 
In this article, we will learn how to find the area of the square using the method overloading.
The area of the square is the square of its sides. We can simply calculate the area of the square using the following formula:
Formula:
Area of the Square: A = a2
Here, a is the side of the square.
Below is the implementation of the above approach:
Example 1:
Showing the method overloading of Area() function which has different parameters of  square sides of different data-type( float , int , double) 
Java
// Java program to find the area of// the square using Method Overloading// of parameters with different datatype// of sides  import java.io.*;  class Square {      // Overloaded Area() function to    // calculate the area of the square    // It takes one double parameter    void Area(double side)    {        System.out.println("Area of the Square: "                           + side * side);    }      // Overloaded Area() function to    // calculate the area of the square    // It takes one float parameter    void Area(float side)    {        System.out.println("Area of the Square: "                           + side * side);    }}  class GFG {      // Driver code    public static void main(String[] args)    {          // Creating object of square class        Square obj = new Square();          // Calling function        obj.Area(10);        obj.Area(3.2);    }}
Area of the Square: 100.0
Area of the Square: 10.240000000000002

Example 2:
Showing method overloading of Area() function which returns the area of different figures like( square, circle, rectangle)
Java
// Java program to find the area of// the multiple shapes// using Method Overloading  import java.io.*;  class Shape {      static final double PI = Math.PI;      // Overloaded Area() function to    // calculate the area of the square    // It takes one float parameter    void Area(float a)    {        float A = a * a;        System.out.println("Area of the Square: " + A);    }      // Overloaded Area() function to    // calculate the area of the circle    // It takes one double parameter    void Area(double a)    {        double A = PI * a * a;        System.out.println("Area of the Circle: " + A);    }      // Overloaded Area() function to    // calculate the area of the rectangle    // It takes two int parameters    void Area(int a, int b)    {        int A = a * b;        System.out.println("Area of the Rectangle: " + A);    }}  class GFG {      // Driver code    public static void main(String[] args)    {          // Creating object of Shape class        Shape obj = new Shape();          // Calling function        obj.Area(10.5);        obj.Area(3);        obj.Area(5, 4);    }}
Area of the Circle: 346.36059005827474
Area of the Square: 9.0
Area of the Rectangle: 20

Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
Initializing a List in Java
Convert a String to Character Array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class