oop - Why is it wrong to supply type parameter in the constructor of a generic class (Java)? -
i'm learning generics in java textbook, talks class genericstack<e> implemented arraylist<e>.
since in order create stack of strings, use
genericstack<string> = new genericstack<string>() or
genericstack<string> new genericstack<>() therefore shouldn't constructor of genericstack defined public genericstack<e>(), or public genericstack<>()? answer no. should defined public genericstack().
why this? constructor can infer type class declaration, given verbosity of java, i'm bit befuddled why <e> or <> formalism gotten rid of here.
there 2 parts this:
the generic type parameter given class definition available within class itself.
you can have generic types specific individual methods (including constructor).
look @ following example:
package snippet; import java.util.arraylist; public class y<e> extends arraylist<e> { public <t> y(t t) { } } where type e available whole of class, type t valid within constructor.
Comments
Post a Comment