Polymorphism



Occasionally I may include affiliate links, which means I may get a commision if you purchase something via that link. Check out my privacy policy for more info.

Interesting problem / assumption I had with polymorphism recently. Take this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<Object> objects = new ArrayList<>();
    
    objects.add(new Object());
    objects.add(new String());
    objects.add(new Date());
    
    for(Object object : objects) {
      method(object);
    }
  }
  
  public static void method(Object o) {
    System.out.println("Object method!");
  }
  
  public static void method(String s) {
    System.out.println("String method!");
  }
}

I thought when the list iterates to the String object, it would go to the method with the String signature. Nope, it goes to the method with the Object signature.

Note:

So it goes by declaration (i.e. variable type) not object type. I tried a few more method calls:

method(new String());
Object object = new String();
method(object);
method((String)object);

The first and last method calls are to the String method, while the middle one is to the Object method.

Technically, it is polymorphism. Polymorphism “refers to a programming language’s ability to process objects differently depending on their data type or class” (http://www.webopedia.com/TERM/P/polymorphism.html), although the source continues to say “More specifically, it is the ability to redefine methods for derived classes” so I get where you’re coming from.

Wikipedia calls the type of polymorphism I’m trying to use ad hoc polymorphism, which “is also known as function overloading or operator overloading” (https://en.wikipedia.org/wiki/Ad_hoc_polymorphism). The main polymorphism entry on Wikipedia says that “subtype polymorphism [is] when a name denotes instances of many different classes related by some common superclass. In the object-oriented programming community, this is often simply referred to as polymorphism” (https://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29)

View on LinkedIn