Area of Rectangle using method overriding in Java

In this Java Example we declares a class named Rect. It contains two integer member variables, l and b (for length and breadth). Since no access specifier is used, these variables are considered public. Public means that any other class can freely access these two members.

We create an object by name rect of class Rect by instantiating the class. When we create a new object, space is allocated for that object and for its member’s l and b. Then the values are assigned to l and b variables as 20 and 60 respectively. After this, the variables l and b of object rect are multiplied and the result is stored in variable a which is then displayed on the screen.

class Rect
{
            int l,b;
}
class CalAreaofRectangle
{
            public static void main (String args[])
            {
                        int a;
                        Rect rect = new Rect();
                        rect.l=20;
                        rect.b=60;
                        a=rect.l*rect.b;
                        System.out.println("Area of Rectangle is : "+a);
            }
}

Area of Rectangle using method overriding in Java

In previous, Java example, the object rect directly allows to assign values to its data members l and b in main() because both are considered public:

rect.l=20;

rect.b=60;

In the following example, the values of the data members l and b are changed with the help of method: setval()

class Rect
{
            int l,b;
            void setval(int x,int y)
            {
                        l=x;
                        b=y;
            }
            int area()
            {
                        return (l*b);
            }
};
class CalculateAreaofRectangle
{
           
            public static void main (String args[])
            {
                        Rect rect = new Rect();
                        rect.setval (50,8);
                        System.out.println("Area of Rectangle is : "+rect.area());
            }
};

Area of Rectangle using method overriding in Java

To set the values of the variables l and b, we use this method:

rect.setval(20,10);

This Java statement calls rect’s setval method with two integer parameters 20 and 10 and that method assigns new values to variables l and b respectively.

We append the method name to an object reference with an intervening period (.). Also, we provide any arguments to the method within enclosing parentheses. If the method does not require any arguments, use empty parentheses. Syntax for calling a method through object:

objectReference.methodN ame(argument List);

or

objectReference.methodName();

ObjectReference must be a reference to an object.

Example:

rect.area();

We can use the dot notation to call the area() method to compute the area of the new rectangle.

In this tutorial, we will learn how to find the area of squares, rectangles, and circles using method overloading. The area of the rectangle is the product of its length and width/breadth. The area of the circle is the product of the square of the radius of the circle and the value of PI. The area of the square is the square of its sides. If a class has multiple methods having the same name but different in parameters, it is known as Method Overloading. But before moving forward if you are not familiar with the concept of method overloading in java, then do check Method Overloading in Java.

Input: Area(3)

Area(3,2)

Area(3.2)

Output:

The area of the square is 9 sq. units.

The area of the rectangle is 6 sq. units.

The area of the circle is 32.15 sq. units.

Let us look at the below examples for a better understanding.

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

In this program, we will see how to find the area of a square, rectangle, and circle using Method Overloading.

Algorithm:

  1. Start
  2. Declare three different classes for rectangle, square, and circle.
  3. Declare two methods of the same name but with a different number of arguments or with different data types.
  4. Call these methods using objects.
  5. Call the corresponding methods as per the number of arguments or their data types.
  6. Display the result.
  7. Stop.

Let us look at the below example for a better understanding of the above algorithm.

//Java Program to Find the Area of Square, Rectangle and Circle using Method Overloading public class Main { //Driver Code public static void main(String[] args) { Rectangle obj = new Rectangle(); // Calling function obj.Area(30, 20); obj.Area(12.5, 4.5); Circle obj1 = new Circle(); // Calling function obj1.Area(3); obj1.Area(5.5); Square obj2 = new Square(); // Calling function obj2.Area(20); obj2.Area(5.2); } } class Square { // Overloaded 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 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 Circle { static final double PI = Math.PI; // Overloaded function to // calculate the area of the circle. // It takes one double parameter void Area(double r) { double A = PI * r * r; System.out.println("The area of the circle is :" + A); } // Overloaded function to // calculate the area of the circle. // It takes one float parameter void Area(float r) { double A = PI * r * r; System.out.println("The area of the circle is :" + A); } } class Rectangle { // Overloaded function to // calculate the area of the rectangle // It takes two double parameters void Area(double l, double b) { System.out.println("Area of the rectangle: " + l * b); } // Overloaded function to // calculate the area of the rectangle. // It takes two float parameters void Area(int l, int b) { System.out.println("Area of the rectangle: " + l * b); } }

Area of the rectangle: 600 Area of the rectangle: 56.25 The area of the circle is:28.274333882308138 The area of the circle is:95.03317777109123 Area of the Square: 400.0

Area of the Square: 27.040000000000003

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

In this program, we will see how to find the area of the square, rectangle, and circle using Method Overloading.

Algorithm:

  1. Start
  2. Declare three methods of the same name but with a different number of arguments or with different data types.
  3. Call these methods using objects.
  4. Call the corresponding methods as per the number of arguments or their data types.
  5. Display the result.
  6. Stop.

Let us look at the below example for a better understanding of the above algorithm.

//Java Program to Find the area of Square, Rectangle and Circle using Method Overloading public class Main { //Driver Code public static void main(String[] args) { CalculateArea ob = new CalculateArea(); ob.area(4); ob.area(10,12); ob.area(5.5); } } class CalculateArea { void area(float x) { System.out.println("The area of the square is "+Math.pow(x, 2)+" sq units"); } void area(float x, float y) { System.out.println("The area of the rectangle is "+x*y+" sq units"); } void area(double x) { double z = 3.14 * x * x; System.out.println("The area of the circle is "+z+" sq units"); } }

The area of the square is 16.0 sq units The area of the rectangle is 120.0 sq units

The area of the circle is 94.985 sq units