• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Doubt on Advantage of Generic Eg over Non-Generic Eg!!!!

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

I have a doubt on the advantage of Generics over the Non-Generic version.

Source : http://java.sun.com/docs/books/tutorial/java/generics/QandE/generics-answers.html





In both Versions, Media is to be defined as an interface along with the Book, Video and Newspapers as the sub-interfaces.

In the Non-Generic Version,

- We have to check the type of the argument resource "Media" before adding any resources to the List resources,
which will throw a compile time error, if add(Media x) is called with any other incompatible argument.

In the Generic Version,

- We have the check "E extends Media" which will help to detect compile time error avoiding run-time exceptions due to incompatible types.

Also, the interface Media with the sub-interfaces will have to be defined in both versions.. then how is the non-generic version helpful!!!

I have read the tutotrial (link shown above) and I fail to understand, the advantage of using genric-version over non-generic one for this example.. can somebody please explain this one....

Tanya.
 
author
Posts: 23956
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
The advantage of using generics is so that you can have the compiler do the type checking -- and not having to do any runtime check, or even casting (and casting correctly).

For simple examples, as you have shown, it is probably of little benefit, as it is unlikely that you would cast incorrectly, in such a simple example. Regardless, why not use generics anyway? It would be kinda annoying to not use generics -- then have to add generics, when the application got complex to the point where you are starting to cast incorrectly.

Henry
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The advantage I see is that your Generic Library class can be created with any class extending Media. Suppose in future you need a DVD class extending Media. You can just say Library<DVD>. Compiler checks that you only DVD to List resources . And retrieveLast() method will return DVD object(you dont need to cast). Hope that is clear.
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic