TypeToken is a class in a Google-provided library (Gson) that parses Json data

Gson TypeToken constructs protect instead of public.

why the TypeToken construction in Gson is so weird?

Using Retrofit to customize convert you see gson and you see TypeToken

I’m curious to see a class that uses the protect modifier. I rarely come across a class that does this, so I’m not sure it works. I asked a question on the SegmentFault and received an answer one of which is the link above, which has a description.

There are also some of their own understanding after receiving the answer after putting forward this question.

  1. Why is itprotectnotpublic? In order not to create objects directly, you have to subclass them.
  2. Why not?abstract? I’m not sure if subclasses have to be created in order to be used in the same package and in the same class
  3. What is forcing subclasses for? Subclasses are forced to be created so that the __ subclass specifies the type __ of the generic declared by the parent classObject), here is the first time I feeljavaUnfortunately, you can’t force generic annotation.

Related knowledge required: Java basics, Java generic erasure