• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to allow list for only two types of objects ?  RSS feed

 
hitesh patil
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to allow list for only two types of objects ?
 
ravi suthar
Greenhorn
Posts: 17
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome to java runch hitesh patil..!
by default you can add any object to list.
but if you want only specific type you can use of generics.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hitesh patil wrote:how to allow list for only two types of objects ?

Are the two types related?

Winston
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless they are related, that is rather contrary to the philosophy of collections, viz, that your collections contain things from a particular category.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a work-around, you could fabricate a "synthetic" type to contain two references: either one to the first type, or one to the second type, and then declare a list for this type.

This type had two constructors: one for each given type.

Not forcibly a very nice or pleasing solution but feasible.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can even make the object contain one type only by careful selection of the constructors
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Unless they are related, that is rather contrary to the philosophy of collections, viz, that your collections contain things from a particular category.

Yeah. I was thinking more along the lines of defining an interface that both of them implement.

Winston
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought you meant something like that, Winston, or a common superclass.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!