Java - Why declare an array as a type of Interface? -
this professor mark weiss in book data structures , algorithm analysis in java
public class binaryheap<anytype extends comparable<? super anytype>>{ private void enlargearray( int newsize ){ anytype [] old = array; array = (anytype []) new comparable[ newsize ]; for( int = 0; < old.length; i++ ) array[ ] = old[ ]; } }
i wondering why declare array type of interface comparable since have convert comparable[]
anytype[]
? design philosophy in there?
the design "philosophy" can't instantiate array of type parameter, have instantiate array type legal. available legal types known method array of object
or of comparable
, , latter captures more knowledge type.
you allowed downcast array of type parameter, , return type has that, downcasting required.
it's "philosophy" of necessity.
Comments
Post a Comment