• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

please help me on Generics!!!!

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers.....

I have read about Generics but i am not getting the concept
or can say the main idea about

1)How generics work in a program..
I have seen many declarations of generics,they differ according to thier
type..can say a List,String..and all.

Can say i am not getting the basic idea...of Generics
please if anyone could explain me with an example in detail but easy to understand language.Please explain the declaration of Generics also.

it will be realy kinda of you.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See dude ..
I think you hvnt studied C++ ..
Consider a situation where your boss asks you to write a sorting algorithm that works for all the data types i.e. pass it say ten chars or ten ints it should work equally well for both.The only thing you can do is make use of generics.You write your class or function in such a way that it changes its behaviour according to what data type you pass to it.

List<String> is a list of Strings as opposed to List<Integer> that would work for Integers.

So List interface is generic,it will act according to what you pass it.
This way the coders of List interface had to write the code only once using generics and it can be used according to need.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dhwani,
Check outthis and this.Read generics topic very well and if you have any specific doubt post it here.

Regards,
Nik
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Asif and Nik....

But Nik i have got a doubt from the first link which you gave its below...



Now Nik please explain me,that above it is shown that in generics List is of type Integer..and it is shown if we pass a String it will work how it wil work with type String?and later it is mentioned that until someone calls a integer method on the String obj in coleection it will blow up.......
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dhwani,

List<Integer> list = new ArrayList<Integer>(); oldClass.alterList(list) ...

class OldClass{
void alterList(List List){
list.add(" a string ! "); // works
}
}



In the above codelist is of type Integer.Look at the alterList() method the argument is not of specific type it means you can pass anything to it and also you can add anything to it whether a String,Integer.This was done to prevent the older code which was not written in generics to be safe.Whenever you add anything other than Integer compiler gives a warning telling that you are trying to add a String to a Integer list.It wont throw a compiler error because just assume that if it throws then all the older code which were not written in generics would blow up.


Regards,
Nik
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nik
for the explanation now i am clear with the above doubt,but please if you dont mind i have some more doubts,i read the topic shown below,from second link but it is explained with a very long example which is making me more confused,can you help me with a simple explanation as well as example.....
Till now i have understood concept of type parameter or can say parameterised type.

Bounded Type parameter

i can understand that with bounded type parameter it creates bounds on types of list,like we can add types of String only,not others........

But while using class with generics as well as methods i am getting jummbled........
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dhwani,
This link is good.Check out.Any doubt if you have post it here


Regards,
Nik
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic