Concrete and constrained parameterized type when designing a type API
I would love to hear from you guys how you decide when to use a specific parameterized type with a restricted parameterized type when designing an API, especially. (which I like the most) class / interface definitions.
For example,
public interface Event<S>{
void setSource(S s);
}
public interface UserEvent extends EVent<User> // OR: UserEvent<S extends User> extends Event<S>
// It will therefore be void setSource(User s);
}
The problem with using a specific parameter is that I cannot bring this compilation benefit that I earn when using setSource () on a new interface, for example
public interface AdminUserEvent extends UserEvent{
void setSource(AdminUser s); // WHERE: AdminUser extends User. This is a method overloading, we also have a void setSource(User s) inherited from UserEvent.
}
To do this, I can perform type checking on the object User
when called AdminUserEvent.setSource()
.
Have you ever had this question while developing your API? And what are the practices or rules that you will follow when this situation arises? Thank.
YC
source to share
If I understand correctly, this has nothing to do with generics in themselves, but rather a parallel hierarchy. B extends A
, BHandler extends AHandler
and AHandler.handle(A)
, but BHandler.handle(B)
.
Yes, I believe it can be done typical using generics. That is, if I understood your problem correctly.
source to share