Actually, the compiler will throw an error if a throw is not possible at all, for example. if the return type of the create() method will be AnotherBaseClass instead of SuperClass .
Since it returns SuperClass , the compiler does not know what will actually be returned, it can also return SomeBaseClass . Therefore, he must believe that you know what you are doing with this act.
Edit:
To get a warning when casting, you can try using a code analysis tool like Checkstyle . Note, however, that these tools most likely cannot or do not check the class hierarchy and, therefore, can only be able to warn the (non-primitive) casts used in general. That way, if you use a library that needs drops (for example, if you use a collection of apache collections that does not yet support generics), you will get many warnings.
After all, programming is still an art, and you still need to know what you are doing.
source share