OOPM JAVA - Calculate the area of different shapes by using method overloading

 Experiment No. 05

Aim: Write a program to calculate the area of different shapes (circle, triangle, rectangle, square) by using method overloading 

Objective: To create a class with methods using different no of parameters and their overloading in real time scenario. 

Outcome: Students successfully created and used method overloading as per requirement of the real time scenario 

Theory: 

Method Overloading 

If a class has multiple methods by same name but different parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program. Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b (int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs. So, we perform method overloading to figure out the program quickly.

Advantage of method overloading?

Method overloading increases the readability of the program.


Different ways to overload the method

There are two ways to overload the method in java

  1. By changing number of arguments
  2. By changing the data type

1) Method Overloading: changing no. of arguments

In this example, two methods are created, first add() method performs addition of two numbers and second add method performs addition of three numbers.


In this example, static methods are created so that don't need to create instance for calling methods.

  1. class Adder{  
  2. static int add(int a,int b){return a+b;}  
  3. static int add(int a,int b,int c){return a+b+c;}  
  4. }  
  5. class TestOverloading1{  
  6. public static void main(String[] args){  
  7. System.out.println(Adder.add(11,11));  
  8. System.out.println(Adder.add(11,11,11));  
  9. }}  


Output :

22

33


2) Method Overloading: changing data type of arguments

In this example, two methods are created that differs in data type. The first add method receives two integer arguments and second add method receives two double arguments.

  1. class Adder{  
  2. static int add(int a, int b){return a+b;}  
  3. static double add(double a, double b){return a+b;}  
  4. }  
  5. class TestOverloading2{  
  6. public static void main(String[] args){  
  7. System.out.println(Adder.add(11,11));  
  8. System.out.println(Adder.add(12.3,12.6));  
  9. }}  


Output :

22

24.9


Method Overloading is not possible by changing the return type of method only?

In java, method overloading is not possible by changing the return type of the method only because of ambiguity. Let's see how ambiguity may occur:

  1. class Adder{  
  2. static int add(int a,int b){return a+b;}  
  3. static double add(int a,int b){return a+b;}  
  4. }  
  5. class TestOverloading3{  
  6. public static void main(String[] args){  
  7. System.out.println(Adder.add(11,11));//ambiguity  
  8. }}  


Output :

Compile Time Error: method add(int,int) is already defined in class Adder


Algorithm:-

  1. Start
  2. Define class Shape with its attributes and methods area() with different type and number of parameters.
  3. Define class Demo 
  4. Create object of Shape class inside class Demo.
  5. Call different overloaded area() methods using object of Shape.
  6. Stop


Code : 

import java.util.Scanner;

public class Overloading 

{

  public static void main(String args[]) 

  {

    double r, b, h;

    float len, bth, s;

    Scanner sc = new Scanner(System.in);

    Overloading obj = new Overloading();

    //area of circle

    System.out.print("Enter radius of circle : ");

    r = sc.nextFloat();

    System.out.println("Area of Circle= " + Area(r));

    System.out.println();

    //area of rectangle

    System.out.print("Enter length of rectangle : ");

    len = sc.nextFloat();

    System.out.print("Enter breadth of rectangle : ");

    bth = sc.nextFloat();

    System.out.println("Area of Rectangle= " + Area(len, bth));

    System.out.println();

    //area of triangle

    System.out.print("Enter base of triangle : ");

    b = sc.nextFloat();

    System.out.print("Enter height of triangle : ");

    h = sc.nextFloat();

    System.out.println("Area of Triangle= " + Area(b, h));

    System.out.println();

    //area of square

    System.out.print("Enter side of square : ");

    s = sc.nextFloat();

    System.out.println("Area of Square= " + Area(s));

    

  }

  public static double Area(double a) {

    double pi =3.142f;

    double circ = pi * a * a;

    return circ;

  }

  public static double Area(float a) {

    float sqr = a * a;

    return sqr;

  }

  public static double Area(float a, float b) {

    float rect = a * b;

    return rect;

  }

  public static double Area(double a, double b) {

    double tri = 0.5 * a * b;

    return tri; 

  } 

}


Output: 

Calculate the area of different shapes by using method overloading


Observations and learning: 

In this program we have seen how to calculate area of geometric shapes with their formula and by use of method overloading in which user had given the inputs.

 

Conclusion: 

Area of Geometric Shapes have been calculated with this program.


Question of Curiosity 

 

  • What is method overloading?

Ans:-Method overloading is a programming technique that allows developers to use the same method name multiple times in the same class, but with different parameters. In this case, we say that the method is overloaded. Listing 1 shows a single method whose parameters differ in number, type, and order.


  • What is method signature? What are the things it consist of?

Ans- In Java, a method signature is part of the method declaration. It's the combination of the method name and the parameter list. The reason for the emphasis on justthe method name and parameter list is because of overloading. It's the ability to write methods that have the same name but accept different parameters.


  • Can we declare one overloaded method as static and another one as non-static?

 :-The answer is, No, you can not override static method in Java, though you can declare a method with the same signature in a subclass. As per Java coding convention, static methods should be accessed by class name rather than an object. In short, a static method can be overloaded, but can not be overridden in Java.


  • How do compiler differentiate overloaded methods from duplicate methods?

Ans:-Compiler uses method signature to check whether the method is overloaded or duplicated. Duplicate methods will have same method signatures i.e same name, same number of arguments and same types of arguments. Overloaded methods will also have same name but differ in number of arguments or else types of arguments.


  • Is it possible to have two methods in a class with same method signature but different return types?

You can not define more than one method with the same name, Order and the type of the arguments. ... The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile time error.


  • In “MyClass” , there is a method called “myMethod” with four different overloaded forms. All four different forms have different visibility ( private, protected, public and default). Is “myMethod” properly overloaded?

:- Cannot be defined


  • Can overloaded methods be synchronized?

:-Yes overloaded Methods can be synchronised.


  • Can we overload main() method?

The answer is, yes, we can overload the main() method. But remember that the JVM 

always calls the original main() method. It does not call the overloaded main() method.


  • Can we declare overloaded methods as final?

Private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one private/final methods of same name but a child class cannot override the private/final methods of their base class.


  • Overloading is the best example of dynamic binding. True or false?

Ans:-Method Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method and in this case the type of the object determines which method is to be executed. The type of object is determined at the run time so this is known as dynamic binding.




Comments