Skip to main content
eLearner.app
Module 2 · Lesson 2 of 24/14 in the course~12 min
Module lessons (2/2)

Methods and Overloading

Methods define the behavior of an object. We can think of them as functions declared inside a class that operate on the object's instance fields or incoming parameters.

Defining a Method

A method in Java consists of:

  1. Access modifier (e.g., public, private).
  2. Return type (e.g., int, String, or void if it doesn't return anything).
  3. Method name in camelCase.
  4. Parameters enclosed in parentheses.
  5. Method body enclosed in curly braces.
Code
class Person {
    String name;

    public Person(String name) {
        this.name = name;
    }

    // Method with return type String
    public String greet() {
        return "Ciao, mi chiamo " + name;
    }
}

Calling a Method

To invoke a method on a created object, we use dot notation .:

Code
public class Main {
    public static void main(String[] args) {
        Person p = new Person("Marco");
        String message = p.greet(); // Calling the method
        System.out.println(message);
    }
}

Method Overloading

In Java, two or more methods within the same class can have the same name, as long as they have a different parameter list (in number, order, or types of parameters). This concept is known as Overloading.

The compiler decides which method to invoke by analyzing the arguments passed during the call.

Code
class Printer {
    // Method to print an integer
    public void printValue(int number) {
        System.out.println("Intero: " + number);
    }

    // Overloaded method to print a string
    public void printValue(String text) {
        System.out.println("Testo: " + text);
    }
}

Parameter Passing by Value

In Java, all parameters are passed by value (pass-by-value). This means that when you pass an argument to a method, Java creates a copy of the variable's value and passes it to the method.

  • For primitive types, the original value outside the method never changes, even if modified inside the method.
  • For objects/references, the reference to the object is copied. This means that while we cannot change the memory address the original object points to, we can modify its internal state (e.g., its fields) and those changes will be visible outside.

Overloading Limitations: The Return Type

It is important to note that you cannot overload a method based solely on the return type. For example, the following code will cause a compilation error:

Code
class Example {
    // ERROR: The signature of the two methods is considered identical
    public int getNumber() { return 42; }
    public double getNumber() { return 42.0; }
}

The compiler needs to distinguish between methods based only on the parameters to decide which version to call at compile-time (overloading is resolved at compile-time).


Try it yourself

Exercise#java.m2.l2.e1
Attempts: 0Loading…

Add a public int add(int a, int b) method to the Calculator class that returns the sum of the two numbers. In the main method, call this method on the calc object and assign the result to an integer variable.

Loading editor…
Show hint

Declare the method as `public int add(int a, int b) { return a + b; }` and invoke it in the main method as `int result = calc.add(5, 10);`.

Solution available after 3 attempts

Exercise#java.m2.l2.e2
Attempts: 0Loading…

Overload the add method in the Calculator class by creating another method that accepts two double parameters and returns their sum.

Loading editor…
Show hint

Write `public double add(double a, double b) { return a + b; }` directly below the first add method.

Solution available after 3 attempts

Exercise#java.m2.l2.e3
Attempts: 0Loading…

Add a public int multiply(int a, int b) method to the Calculator class that returns the product of the two numbers. In the main method, call this method on the calc object and assign the result to an integer variable.

Loading editor…
Show hint

Declare the method as `public int multiply(int a, int b) { return a * b; }` and invoke it in the main method as `int result = calc.multiply(5, 10);`.

Solution available after 3 attempts