BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to find area of Geometric figures using method Overloading

Last Updated: September 9, 2017 by Chaitanya Singh | Filed Under: Java Examples

This program finds the area of square, rectangle and circle using method overloading. In this program we have three methods with same name area(), which means we are overloading area() method. By having three different implementation of area method, we are calculating the area of square, rectangle and circle.

To understand this program you should have the knowledge of following Core Java topic:
Method Overloading in Java

Example: Program to find area of Square, Rectangle and Circle using Method Overloading

class JavaExample
{
    void calculateArea(float x)
    {
        System.out.println("Area of the square: "+x*x+" sq units");
    }
    void calculateArea(float x, float y)
    {
        System.out.println("Area of the rectangle: "+x*y+" sq units");
    }
    void calculateArea(double r)
    {
        double area = 3.14*r*r;
        System.out.println("Area of the circle: "+area+" sq units");
    }
    public static void main(String args[]){
        JavaExample obj = new JavaExample();
       
        /* This statement will call the first area() method
         * because we are passing only one argument with
         * the "f" suffix. f is used to denote the float numbers
         * 
         */
	 obj.calculateArea(6.1f);
	   
	 /* This will call the second method because we are passing 
	  * two arguments and only second method has two arguments
	  */
	 obj.calculateArea(10,22);
	   
	 /* This will call the second method because we have not suffixed
	  * the value with "f" when we do not suffix a float value with f 
	  * then it is considered as type double.
	  */
	 obj.calculateArea(6.1);
    }
}

Output:

Area of the square: 37.21 sq units
Area of the rectangle: 220.0 sq units
Area of the circle: 116.8394 sq units

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java program to reverse a number using for, while and recursion
  3. Automorphic Number Program in Java
  4. Peterson Number Program in Java
  5. Java Program to Print Sandglass Star Pattern

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap