I am getting error while casting Set < Supertype > to Set< Subtype >
I am getting following error :
Cannot cast from Set< AppUserRole > to Set< AppUserRoleEntity >
interface AppUserRole {
void m1();
}
class AppUserRoleEntity implements AppUserRole {
@Override
public void m1() {
}
}
interface AppUser {
void m2(Set<AppUserRole> abc);
}
class AppUserEntity implements AppUser {
Set<AppUserRoleEntity> role;
@Override
public void m2(Set<AppUserRole> abc) {
this.role = (Set<AppUserRoleEntity>) abc;// Compilation fails here
}
}
After reading PECS I have modified my code like below
Set< ? super AppUserRoleEntity> role;
But not on getter method of same class I am getting warning
Type safety: Unchecked cast from Set< capture#2-of ? super AppUserRoleEntity > to Set
@Override
public Set<AppUserRole> getAppUserRoles() {
return (Set<AppUserRole>) appUserRoles;
}
Can some one tell me how to fix this problem ? Please help