3

How is the syntax in Java for GenericExample<ItemType (extends or equal to) Object>?

Thanks, Adam.

Update:

Thanks for all your replies. The answers here are more related to the use of the generics in code, I would like to discuss the deceleration implementation, for example:

class GenericExample<ItemType (extends or equal to) ParentType> { } class Inherited<ParentType> extends GenericExample<ParentType> { /* The type parameter in this class does not compile. I would like to find a work around to make something like this to work. I would like to have ParentType=JComponent , and thus to specify that the Inherited class uses JComponent for needed flexibility*/ } 

I hope this makes more sense...

Adam.

3
  • I'm pretty sure that it's not possible in Java. If I understood it correctly, it's like when you in C# declare a "GenericExample< T > where T: SomeParentClass" Commented Jan 19, 2011 at 16:12
  • @djechelon: You would be wrong in thinking that it isn't possible. =) Commented Jan 19, 2011 at 17:19
  • I never use a single letter to refer to a parameter type, I always use a mining-full Type name, it makes it easier to keep track over what the object needs to do, or what it is. Commented Jan 20, 2011 at 11:17

2 Answers 2

4

You already got it:

GenericExample<ItemType extends MyObject> 

A first guide on generics can be found here: http://download.oracle.com/javase/1.5.0/docs/guide/language/generics.html

Sign up to request clarification or add additional context in comments.

3 Comments

shouldn't that at least include a Generic parameter? Like: GenericExample<T extends MyObject> . Still bumping, as I think you have provided what the OP needed.
ItemType is a generic parameter. But you are right, in Java, common usage is to use One letter (ususally T) for generic parameter.
Also remember that this is only enforced at compile time and will be replaced with type casts at runtime due to type erasure in Java.
1
GenericExample<T extends Object> 

1 Comment

You are correct here, but I think it's more clear if you use some type other than Object as the type bound, since what you show here is equivalent to just GenericExample<T>.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.