Shadab Wadiwala wrote:1) Regarding generic class
So for example,,
here is a class
now here i understood that <T> is type it can be anything say,Integer,Long or say , Animal (provided Animal is a class)
so we can write
but how to use this class ..
The second case is not doing what you think it does. The first case declares a type T, which can be any type. The second case declares a type Integer, which can be any type. "Integer" is a type variable, and not refering to the Integer class.
why at all we define generic type after class name???
In these two examples, there is no reason to -- as you don't use the types anywhere.
When you declare your class as rentalGeneric<Integer>, this doesn't mean that your class rentalGeneric has type java.lang.Integer. Try this
Basically you are just using Integer instead of T but that doesn't make your Integer the java.lang.Integer class. So don't use anything like Integer or String with your class declaration as it doesn't mean what you think it means.
As far as methods go, your code doesn't use generic methods very well. You've shown list.add as a statement in your method but where is this list declared. If a generic method is used in conjunction with generic class, then it works like this
Now if you want to use this class, then here's how you can use it
So here you declare your class to have Integer as the type of T, so you pass the display method a List of Integers. If you want to use a generic method which declares its own type, then here's how you can do it.
This method will accept an array list of a specific type and return a LinkedList of the same type. This is how you can call it