UIL CS: OOP & Generics

This is part of the UIL CS study series. See also:

Main topic list: https://www.uiltexas.org/files/academics/UILCS-JavaTopicList2526.pdf


11. OOP: Method Dispatch + Generics Instancing Errors

A. Method dispatch (dynamic dispatch)

In Java:

  • Overloading resolved at compile time
  • Overriding resolved at runtime based on actual object type

Example:

class A {
    void f() { System.out.println("A"); }
}
class B extends A {
    @Override void f() { System.out.println("B"); }
}

A obj = new B();
obj.f(); // prints "B"

Even though the variable type is A, the object is B, so B.f() runs.

B. Generics: instancing examples (error vs not)

Example that causes an error

List<int> x = new ArrayList<int>(); // ERROR

Correct:

List<Integer> x = new ArrayList<>();

Another common generics error (invariance)

List<Integer> a = new ArrayList<>();
List<Number> b = a; // ERROR

Even though Integer is a Number, List<Integer> is NOT a List<Number>.

Fix using wildcards:

List<? extends Number> b = a; // OK (read-only-ish)

Example that is OK

List<Number> nums = new ArrayList<>();
nums.add(3);    // Integer auto-boxed
nums.add(2.5);  // Double

Auto-unboxing occurs whenever the compiler sees an object of type wrapper class passed into a primitive type. The Double.doubleValue() and Integer.intValue() methods are used for manual auto-unboxing.