Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro Java
Pro Java Expert level Java questions not about a specific book. Please indicate your version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Java section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 21st, 2008, 12:33 PM
Registered User
Join Date: Apr 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default unclear use of generic types

what are genric types used for?

are genric types only used for creating type safe collections. cant we just use them as simple type safe classes for exmaple can we use genric types in the following manner

consider the following scenario :

in a particular electronics trading company they send order to suppliers. now the products they deal in are TV,REFERIGERATOR,WASHING MACHINE all provided by different suppliers. i.e. different orders are prepared for each item.

the TV specification is defined by class TV. similarly REFERIGERATOR specifications are defined by class FRIDGE. and WASHING MACHINE by class WASHMACH. but the order for all the three has same details

1. item name
2. code
3. quantity
4. addItem()
5. editOrder()
6. SubmitOrder()

why cant we create a genric class named ORDER and keep passing type parameters for TV,FRIDGE,WASHMACH.

now here we are niether creating any linked list or binary tree or some kind of array list. i mean no collection is used. still using genric class beacuse it makes the work easier.

but is it necessary that generic types are just used for creating type safe collections?
Old March 5th, 2009, 03:14 AM
Friend of Wrox
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post

Generics make your code look simple by eliminating type specific code.
That doesn't mean generics are restricted to be used with collections. You can use it any where you've type specific code and you want to avoid it.
Refer any good material about generics you'll know the real power of generics :-)

Some info about generics
- Rakesh

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i make this generic girija45701 C# 0 November 29th, 2008 04:32 AM
Generic Date Calculation arnabghosh Javascript 0 April 10th, 2008 01:56 AM
Generic List error ferahl C# 2 October 30th, 2007 03:28 AM
Unclear instructions in chapter 1 page 15 Merocal BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 17 January 14th, 2007 06:54 PM
Problem with generic collections demitry Visual Studio 2005 1 November 21st, 2005 05:30 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.