DEV Community

Gowtham
Gowtham

Posted on

Java - Methods & Method Overloading

1.Method in Java:

A method is a block of code that performs a specific task. We can call a method whenever we need to execute that task.

Methods help us avoid repeating the same code multiple times.

Example:

`public class Main {

static void greet() {
    System.out.println("Hello, Welcome to Java!");
}

public static void main(String[] args) {
    greet();
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

Hello, Welcome to Java!

Explanation

  • static allows us to call the method using the class without creating an object.
  • void means the method does not return a value.
  • greet() is the method name.
  • System.out.println() displays the message.
  • greet(); calls the method.

2.Types of Methods in Java

Methods can be categorized in different ways. Two common types are:

1.Predefined Methods

Predefined methods are already provided by Java's libraries.

Example:

`public class Main {
public static void main(String[] args) {
System.out.println("Hello Java!");

    int result = Math.max(10, 20);
    System.out.println(result);
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

Hello Java!
20

Here, println() and Math.max() are predefined methods.

2.User-Defined Methods

User-defined methods are created by programmers to perform specific tasks.

Example:

`public class Main {

static void add() {
    int a = 10;
    int b = 20;

    System.out.println(a + b);
}

public static void main(String[] args) {
    add();
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

30

Here, add() is a user-defined method.

3.Methods with Parameters and Return Values:

A method can accept parameters and return a result.

Example:

`public class Main {

static int add(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
    int result = add(10, 20);
    System.out.println(result);
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

30

Explanation

  • int a and int b are parameters.
  • return a + b; returns the sum.
  • add(10, 20) calls the method with two arguments.
  • The returned result is stored in the result variable.

4.Method Overloading in Java:

Method overloading means creating multiple methods with the same name but different parameter lists within the same class.

The methods can have different numbers of parameters or different parameter types.

Method overloading makes code easier to understand because related operations can share the same method name.

Example 1: Different Number of Parameters

`public class Main {

static int add(int a, int b) {
    return a + b;
}

static int add(int a, int b, int c) {
    return a + b + c;
}

public static void main(String[] args) {
    System.out.println(add(10, 20));
    System.out.println(add(10, 20, 30));
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

30
60

Here, both methods have the name add, but one accepts two parameters and the other accepts three.

Java selects the appropriate method based on the arguments supplied.

Example 2: Different Parameter Types

`public class Main {

static int display(int a) {
    return a;
}

static double display(double a) {
    return a;
}

public static void main(String[] args) {
    System.out.println(display(10));
    System.out.println(display(10.5));
}
Enter fullscreen mode Exit fullscreen mode

}`

Output:

10
10.5

The first method accepts an int, while the second accepts a double.

Important: Changing only the return type is not enough to overload a method. The parameter list must be different.

5.Advantages of Methods and Method Overloading:

  • Code reusability: Write code once and call it multiple times.
  • Readability: Give meaningful names to operations.
  • Less duplication: Avoid writing similar code repeatedly.
  • Flexibility: Method overloading supports different input types and numbers of arguments.
  • Easy maintenance: Organize programs into smaller, manageable parts.

Top comments (0)