• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Declarations

 
Faraah Dabhoiwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain me how to make our own Generic Class and Generic Methods?

its quite confusing
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us know what you find confusing and we can narrow down on the problem.

Have you used some of the generic collections ?
 
Faraah Dabhoiwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Regarding generic class
Ok,,

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 ..
why at all we define generic type after class name???

also there is another example where there are two generic types declared after class name




1) Regarding generic method




how to use this method?
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shadab Wadiwala wrote:1) Regarding generic class
Ok,,

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.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, the Sun tutorial on Generics may be a good place to start ...

http://java.sun.com/docs/books/tutorial/java/generics/index.html

Henry
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic