Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor in Generic Class

 
Charles Chikito
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is a code snippet for a generic class...



When we create an instance of this class, we will do as follows (for an example):



Why didn't we then write the constructor as:


I mean the <T> in public ABC<T>(T a, T b) {

Thanks

_Charles
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already defined the generic at class level. It is not necessary to redefine it at method level. Does that answer your question ?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well to make generic constructors or methods its not legal to define them like we do the class. Instead we do it with the boundry type like public <T> ABC(T a, T b). Why is it like this??? hmm think about it? If the Generic ABC class has a reference variable to itself then how it should be defined with the generic...... private ABC<T> a so doesn't it look similar to the Constructor. Well some restrictions are We cannot use it after the constructor and cannot be used to create an instance.

Well as Deepak said , its not necessary has it has already been defined in the class level. The boundary is used when it is not defined for the class i.e for methods.
 
Charles Chikito
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already defined the generic at class level. It is not necessary to redefine it at method level.


I got it. Thanks
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitish Bangera wrote:
If the Generic ABC class has a reference variable to itself then how it should be defined with the generic...... private ABC<T> a so doesn't it look similar to the Constructor. Well some restrictions are We cannot use it after the constructor and cannot be used to create an instance.


could you explain this with a code please......

thanks in advance my friend.........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic