METHOD OVERLOADING IN JAVA PROGRAMMINGsteemCreated with Sketch.

in Popular STEM4 years ago

Overloading is one of the most important concept in object oriented programming. Method overloading means that you can use the same method name to perform different operations provided that the data type or argument passed to the method is different from others. The main advantages of method overloading is that it allows the flexibility for programmers to use same method name for different operation so that code can be readable and very clean. Here's the simple java program demonstrating method overloading.

package Lesson1;

public class MethodOverloading {

    public static void main(String[] args) {
        
        System.out.println("The area of rectangle with length 8 and breadth 5 is "+ Area(8,5));
        System.out.println("The area of cuboid with length 8, breadth 5 and height 4 is "+ Area(8,5,4));
        
    }

    public static int Area(int length, int breadth) {
        return (length*breadth);
    }
    
    public static int Area(int length, int breadth, int height) {
        return (length*breadth*height);
    }
}

You can see I created a method named Area() to calculate the area of rectangle and the cuboid. And the parameters passed to them are different. The output of this code when executed is:


Screenshot_1.png

Coin Marketplace

STEEM 0.28
TRX 0.13
JST 0.032
BTC 61626.58
ETH 2940.28
USDT 1.00
SBD 3.66