0

friends! I decided to read more about generics and I am stuck in wiki.

A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. It defines one or more type variables that act as parameters. A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized types share the same class at runtime.

So, we have sentence

A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section.

How I understand it? When we make instance of our generic class with concrete type argument (creation of parameterized type I mean, Box<Integer> test = new Box<Integer>();), in this moment, this parameterized type make invocation of type parameter in itself class Box<T>{}.

Do I understand it right?

1 Answer 1

2

That is correct, a generic type is a type for which the type of one or more fields is defined on use rather than in declaration.

So if you had a List<T>, you can have a List<Cat> and List<Dog>

Or for a dictionary

AClassWithTwoDifferentTypes<K,V>
AClassWithTwoDifferentTypes<Integer,String>
AClassWithTwoDifferentTypes<Integer,Integer>
AClassWithTwoDifferentTypes<String,String>
Sign up to request clarification or add additional context in comments.

4 Comments

Please don't use Dictionary as example, as its usage is discouraged (due to its implementation HashTable). How about just Map ?
I wasn't referring to any particular dictionary implementation, though I do recognize that is a common collection name. If it helps you sleep better you can think of it as just the map interface.
From javadocs of Dictionary : NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class. So giving advice to OP using obsolete class didn't help me sleep better.
You misunderstand, I happened to choose dictionary as a name, I don't care whether that's actually a thing. Lots of languages call a Map a Dictionary, I don't happen to care if that's a name in use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.