• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor in Generic Class

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.........
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic