Understanding Method Overloading

Method overloading is a feature in Java that allows a class to have multiple methods with the same name but with different parameters. This provides flexibility in designing classes by allowing developers to create methods that perform similar tasks but with variations in input parameters. Method overloading is based on the concept of polymorphism, where the same method name can behave differently depending on the context of its usage.

  • Basic Syntax of Method Overloading:
  • public class ClassName {
        // Method definition 1
          returnType methodName(parameterType1 param1, parameterType2...) {
           // Method body
          }
           // Method definition 2 (overloaded version)
          returnType methodName(parameterType1 param1, parameterType2...) {
           // Method body
          }
           // Additional overloaded methods can be defined similarly
    }

    1.public class ClassName { ... }: The method overloading syntax is typically defined within a class.


    2.Return Type: Specifies the data type of the value that the method returns. Use 'void' if the method does not return any value.

    3.Method Name:Should be a valid identifier following Java naming conventions.

    4.Parameter List:Specifies the data type and name of each parameter that the method accepts. Multiple parameters are separated by commas.

    5.{ ... } This block contains the method body, where the actual functionality of the method is defined..


  • Real-life Examples of Method Overloading:

  • Question 1. Write a class with name Area using function overloading that computes the area of a parallelogram, a rhombus and a trapezium. Area of a parallelogram (pg) = b*h , Area of a rhombus (rh) = 1/2*d1*d2 (d1 & d2 are the diagonals) , Area of a trapezium (tr) = 1/2*( a + b)* h (where a and b are the parallel sides, h= perpendicular distance the parallel sides)

    Solution
    import java.util.*; class Area1 { void ar(int b,int h) { int a1; a1=(b*h); System.out.println("The area of a parallelogram is:"+a1); } void ar(double d1,double d2) { double a2; a2=(0.5*d1*d2); System.out.println("The area of a rhombus is:"+a2); } void ar(int a,int b,int h) { double a3; a3=(0.5*(a+b)*h); System.out.println("The area of a trapezium is:"+a3); } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int base,height; double diag1,diag2; int a,b,h; Area1 ob=new Area1(); System.out.println("Enter the base and height of the parallelogram:"); base=sc.nextInt(); height=sc.nextInt(); ob.ar(base,height); System.out.println("Enter the 1st and 2nd diagonal of a rhombus:"); diag1=sc.nextDouble(); diag2=sc.nextDouble(); ob.ar(diag1,diag2); System.out.println("Enter the parallel sides and perpendicular distance the parallel sides of a trapezium:"); a=sc.nextInt(); b=sc.nextInt(); h=sc.nextInt(); ob.ar(a,b,h); } }

    OUTPUT:
    Enter the base and height of the parallelogram:
    10
    20
    The area of a parallelogram is:200
    Enter the 1st and 2nd diagonal of a rhombus:
    10
    5
    The area of a rhombus is:25.0
    Enter the parallel sides and perpendicular distance the parallel sides of a trapezium:
    5
    5
    10
    The area of a trapezium is:50.0


    Question 2..Write a class with the name Perimeter using function overloading that computers the perimeter of a square, a rectangle and circle. Perimeter of a square =4*s , Perimeter of a rectangle = 2*(l+b), Perimeter of a circle= 2* π * r.

    Solution
    import java.util.*; class FunO4{ void perimeter(int s){ double ps; ps=4*s; System.out.println("Perimeter of a square="+ps); } void perimeter(int l,int b){ int pr=2*(l+b); System.out.println("Perimeter of a rectangle="+pr); } void perimeter(double r){ double pc; pc=2*22.7*r; System.out.println("Perimeter of a circle="+pc); } public static void main(String args[]){ Scanner sc=new Scanner(System.in); int s,l,b; double r; System.out.println("Enter s,l,b,r"); s=sc.nextInt(); l=sc.nextInt(); b=sc.nextInt(); r=sc.nextDouble(); FunO4 ob=new FunO4(); ob.perimeter(s); ob.perimeter(l,b); ob.perimeter(r); } }

    OUTPUT:
    Enter s,l,b,r
    10
    20
    5
    15
    Perimeter of a square=40.0
    Perimeter of a rectangle=50
    Perimeter of a circle=681.0

  • Practice Exercises:
  • 1. Write a program to interchange the value (swap) of the two numbers a and b and display the result after swapping. Use overload function display (int,int) and display(float,float) for swapping integer and float type values.
    2. Write a class with the name volume using function overloading that computes the volume of a cube, a sphere and a cuboid. Volume of a cube (vc) = s*s*s , Volume of a sphere (vs) = 4/3* π *r*r*rVolume of a cuboid (vcd) = l*b*h.

  • Conclusion:
  • In conclusion, method overloading in Java is a powerful feature that promotes code clarity, flexibility, and reusability by allowing methods with the same name but different parameter lists. It facilitates cleaner code organization and contributes to polymorphism, enhancing the overall maintainability of Java programs.