79435711

Date: 2025-02-13 09:49:16
Score: 1.5
Natty:
Report link

You don´t get a Compile Error because you are casting here:

public <T extends SuperService> T getServiceType(ServiceType type){
    switch (type){
        case CUSTOMER:return (T) new CustomerServiceImpl();
        case ITEM:return (T) new ItemServiceImpl();
        case ORDER:return (T) new OrderServiceImpl();
    }
    return null;
}

With type erasure in place you basically casting all the Impls to SuperService.

Now since SuperService is an interface you won´t get a compile error, but you will get a runtime error!

It basically boils down to this:

Casting to unrelated interfaces does not give you a compile error.

More info can be found here: Why does it compile when casting to an unrelated interface?

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: berse2212