Java Program to Find Area of Square, Rectangle and Circle using Method Overloading

This is a Java Program to Find Area of Square, Rectangle and Circle using Method Overloading.

We declare three methods of same name but with different number of arguments or with different data types. Now when we call these methods using objects, corresponding methods will be called as per the number of arguments or their datatypes.

Here is the source code of the Java Program to Find Area of Square, Rectangle and Circle using Method Overloading. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. class OverloadDemo
  2. {
  3.     void area(float x)
  4.     {
  5.         System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
  6.     }
  7.     void area(float x, float y)
  8.     {
  9.         System.out.println("the area of the rectangle is "+x*y+" sq units");
  10.     }
  11.     void area(double x)
  12.     {
  13.         double z = 3.14 * x * x;
  14.         System.out.println("the area of the circle is "+z+" sq units");
  15.     }
  16. }
  17. class Overload 
  18. {
  19.      public static void main(String args[]) 
  20. 	{
  21. 	   OverloadDemo ob = new OverloadDemo();
  22. 	   ob.area(5);
  23. 	   ob.area(11,12);
  24. 	   ob.area(2.5);
  25.         }
  26. }

Output:

$ javac OverloadDemo.java
$ java OverloadDemo
 
the area of the square is 25.0 sq units
the area of the rectangle is 132.0 sq units
the area of the circle is 19.625 sq units

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.