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:

  1. the generic type parameter given class definition available within class itself.

  2. 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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -