Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is my code type safe?

I am pretty sure this is type safe, but just wanted to check as Eclipse is asking me to put a @SuppressWarnings("unchecked") annotation.

Map<String, IFace> faces;

public <T extends IFace> T getFace(String key)
{
    return (T) faces.get(key);
}
like image 832
Cheetah Avatar asked Aug 11 '13 13:08

Cheetah


1 Answers

It is not type safe. You are upcasting here so if you cast to an incompatible derived class you will come across an error at some point.

For example if A_Face and B_Face both extend IFace. You might at some point be casting a B_Face as an A_Face which is not type safe.

like image 62
DrYap Avatar answered Oct 04 '22 18:10

DrYap