• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Generic Declarations

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain me how to make our own Generic Class and Generic Methods?

its quite confusing
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic