Monday, May 18, 2009

Type Safe Classes

We always try to use type safe collections where ever possible, for example

What would it take to implement a type safe Collection like Stack ? Below is the implementation of type safe Stack class


Why I have used "@SuppressWarnings("unchecked")", and still claiming it to be type safe ? First, its type safe for the client using this Stack, its pushes and pops element of type E. Second, we know what we are doing, we are typecasting Object[] to E[]; hence a "@SuppressWarnings("unchecked")."

No comments: