I have 1 abstract class: Model<E>
with 2 implementations: ItemModel
and ItemLogModel
. The constructor of ItemLogModel
is protected, but it is still accessible from ItemModel
. I expect the protected constructor to only be accessible by subclasses of ItemLogModel
. Can someone explain to me why this is possible?
Code:
public abstract class Model<E> implements IModel {
private final Repository<? extends IModel, E> _repository;
protected E _entity;
protected Model(Repository<? extends IModel, E> repository, E entity)
this._repository = repository;
this._entity = entity;
}
...
}
public class ItemModel extends Model<Item> implements IItem {
...
@Override
public void setStatus(Status status, String logMessage) throws Exception {
super._entity.setStatus(status.toString());
super.update();
// CALLING protected constructor here!
new ItemLogModel(this, logMessage);
}
}
public class ItemLogModel extends Model<ItemLog> implements IItemLog {
protected ItemLogModel(ItemModel itemModel, String message) throws Exception {
super(REPOSITORY, new ItemLog());
super._entity.setAccessPointBean(itemModel.getEntity());
super._entity.setStatus(itemModel.getStatus().toString());
super._entity.setMessage(message);
super.save();
}
}