Why roll up to Closeable first?

While reading some Java source, I came across this line:

((Closeable) some_obj).close();

some_obj is obviously an instance of a class that implements the Closeable interface. My question is: why do they first throw some_obj into Closeable before calling close (). I can not just do

some_obj.close();
+3
source share
3 answers

Assuming the compile-time type some_objimplements Closeable, then yes, you could.

, , , , Closeable, - ( Object) "" (, ).

, # , , - . , - , , .

+7

some_obj close(), .

+3

, . ( , .)

, , , . , some_obj . , ...

() , , . , Java JIT . , , , .

+2

Source: https://habr.com/ru/post/1755675/


All Articles