Key Concepts:

Method Signature:

  • The method signature is a combination of the method name and its parameter list.
  • It does not include the return type.

Parameter Types and Order:

  • In method overloading, two methods with the same name must have different parameter types or a different number of parameters.
  • The order of parameters matters

Example:

public class EmployeeDatabase {

    // Method signature: addEmployee(String, double)
    public void addEmployee(String name, double salary) {
        // Implementation to add an employee with name and salary
        System.out.println("Added employee: " + name + " with salary: $" + salary);
    }

    // Method signature: addEmployee(String, int)
    public void addEmployee(String name, int employeeId) {
        // Implementation to add an employee with name and employee ID
        System.out.println("Added employee: " + name + " with ID: " + employeeId);
    }

    // Method signature: addEmployee(String, double, String)
    public void addEmployee(String name, double salary, String department) {
        // Implementation to add an employee with name, salary, and department
        System.out.println("Added employee: " + name + " with salary: $" + salary + " in " + department + " department");
    }

    public static void main(String[] args) {
        EmployeeDatabase database = new EmployeeDatabase();

        // Adding employees using different method signatures
        database.addEmployee("Alice", 60000);  // Uses addEmployee(String, int)
        database.addEmployee("Bob", 75000.50); // Uses addEmployee(String, double)
        database.addEmployee("Charlie", 90000, "Engineering"); // Uses addEmployee(String, double, String)
    }
}
EmployeeDatabase.main(null)
Added employee: Alice with ID: 60000
Added employee: Bob with salary: $75000.5
Added employee: Charlie with salary: $90000.0 in Engineering department

In this example:

  • We have a class EmployeeDatabase with three addEmployee methods, each having a different method signature.
  • The first method takes a String name and a double salary.
  • The second method takes a String name and an int employeeId.
  • The third method takes a String name, a double salary, and a String department.

By having different method signatures, we can effectively overload the addEmployee method, allowing it to handle various scenarios with different parameter types and orders. The main method demonstrates how we can use each version of the addEmployee method based on the specific information available when adding employees to the database.

Q24 Adding overloaded method one to SomeClass (focus: signature)

Consider the following code:

public class SomeMethods {
    public void one(int first) { /* implementation not shown */ }
    public void one(int first, int second) { /* implementation not shown */ }
    public void one(int first, String second) { /* implementation not shown */ }
}

Which of the following methods can be added to the SomeMethods class without causing a compile-time error?

I

public void one(int value) { /* implementation not shown */ }

This method cannot be added because it has the same signature as the first existing method (public void one(int first)).

II

public void one(String first, int second) { /* implementation not shown */ }

This method can be added because its signature is different from the existing methods. It has different parameter types and their order.

III

public void one(int first, int second, int third) { /* implementation not shown */ }

This method can be added because it has three int parameters, which is different from the existing methods.

Therefore the correct answer is D) II and III only

HACKS

  • Create a Java class named CustomMethods.
  • Add three methods with the name customMethod to the class, each with different parameter lists.
  • Create a Subclass called ChangeMethods.
  • Override one of the methods without any errors