Method overloading allows defining multiple methods with the same name but different parameter types. This approach keeps the code clean and ensures that each Method handles a distinct responsibility without mixing logic.
class Screen { ... }
class File { ... }
class Dolphin {
// Method to print to a screen
public void print(Screen screen) { ... }
// Method to print to a file
public void print(File file) { ... }
public static void main(String[] args) {
Dolphin dolphin = new Dolphin();
Screen screen = new Screen();
File file = new File();
dolphin.print(screen); // Calls print(Screen)
dolphin.print(file); // Calls print(File)
}
}