Phoenix
Phoenix

Reputation: 8923

What does this generics code below do ?

I am unable to understand what the generics code below does. I am new to generics so would appreciate all the help i can get!

Public abstract class AMetadata< C extends CMetadata, P extends PMetadata, R extends   RMetadata> extends GEntityMetada<C> 
{ 
 // class does stuff here
}

Could anyone explain how the classes are related ?

Upvotes: 2

Views: 87

Answers (4)

StriplingWarrior
StriplingWarrior

Reputation: 156524

This specifies that the AMetadata class will deal with three generically-defined types, each of which are guaranteed to extend a different type (CMetadata, PMetadata, and RMetadata, respectively).

Furthermore, the AMetadata class itself extends the GEntityMetada generic class, with its generic argument being the first generic argument type (C, which extends CMetadata) passed to AMetadata.

To say how the classes are related would require more knowledge of the code base than this snippet provides. For example, it is possible (though unlikely) that a single type could actually extend CMetadata, PMetadata, and RMetadata, and that type could therefore be used as an argument to all three classes. But there is nothing in this generic definition to indicate that there has to be any relationship between these three classes.

The only other information you can really get from this is that a type that extends CMetadata is a valid generic parameter for the GEntityMetada class. Whether GEntityMetada requires its argument to extend CMetadata is unclear.

Upvotes: 5

msi
msi

Reputation: 2639

It just states what is the template of AMetadata you are willing to create. I'll use short names for clearness. So assume your code looks like this:

public abstract class AM <C extends CM, P extends PM, R extends RM> extends GM<C>

This means that you can create AM object but you have to say what type of elements it should have. Here is the example:

AM<CMChild, PM, RMChild> extends GM<CMChild>

Upvotes: 0

user949300
user949300

Reputation: 15729

The class AMetaData extends a class GentityMetad. So it has a generic parameter C. I can't tell if GentityMetad puts any restrictions on C.

For AMetadata, there is an additional requirement on C: it must extend CMetadata.

In addition, the class AMetaData has two more generic types, P and R, which must extend PMetadata and RMetadata respectively. These are unrelated to GEntityMetad.

Upvotes: 0

Beachwalker
Beachwalker

Reputation: 7915

The generic type params are subtype of a class, e.g. C is a subtype of CMetadata in your example.

Upvotes: 0

Related Questions