0

I'm curious to know whether it could be considered as method overloading if a class implements two or more interfaces with similar methods. If not then what's the right terminology?

Take an example

public interface I1 {
  int method1(String input);
}

public interface I2 {
  void method1(int input);
}

public class C1 implements I1, I2 {
  public int method1(String input){ return 0;}

  public void method1(int input){}
}
Lino
  • 19,604
  • 6
  • 47
  • 65
Tech Sawy
  • 91
  • 7
  • 1
    The compiler knows which one is being implemented by the signature. For class C1, as long as both methods are implemented, then that is fine. You could equally have both methods defined in a single interface, it would make no difference. So yes method 1 is overloaded. – jr593 Jan 26 '21 at 07:41
  • Does this answer your question? [Java overloading vs overriding](https://stackoverflow.com/questions/837864/java-overloading-vs-overriding) – Hulk Jan 26 '21 at 07:54
  • Typically I have seen / used scenarios wherein a super class / interface had overloaded methods. Then I thought what would be the right terminology if above scenario happens. Based on your inputs I think it is safe to say it is overloading. – Tech Sawy Jan 26 '21 at 08:01
  • *FYI:* 1) `interface` is spelled in all lowercase. --- 2) The methods in `C1` *must* be declared `public`. --- 3) The methods in `C1` *should* be annotated with `@Override`. – Andreas Jan 26 '21 at 08:35
  • @Andreas it was just an example to show what I am asking. You can't be serious about it. – Tech Sawy Jan 27 '21 at 08:05

1 Answers1

6

Overloading boils down to:

In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading.

From here.

So, obviously, your class C1 does overload method1(). The fact that it does that in order to override the two methods doesn't change that. It doesn't matter to the definition of overloading if overriding happens, too.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Note that if both interface methods are `default`, and you don't override them in `C1`, the methods are still *overloaded* by `C1`. – Andreas Jan 26 '21 at 08:37