Reputation: 75
How can I create addWorker in interface? OfficeWorker inherits from Worker and now I want to make interface for few Workers. I don't know how to do it correct. void addWorker(Worker worker) and void addWorker(Worker worker) are wrong.
Interface
public interface TypeOfWorker {
void addWorker(Worker worker);
}
OfiiceWorker
public class OfiiceWorker extends Worker implements TypeOfWorker {
private int officeID;
private int intelectl;
private List<OfiiceWorker> ofiiceWorkers = new ArrayList<>();
public OfiiceWorker(String name, String surname, int age, int experience, String street, int building,
int local, String city, int officeID, int intelectl) {
super(name, surname, age, experience, street, building, local, city);
this.officeID = officeID;
this.intelectl = intelectl;
}
@Override
public void addWorker(OfiiceWorker ofiiceWorker) {
ofiiceWorkers.add(ofiiceWorker);
}
}
Worker
public abstract class Worker {
private int identifier;
private String name;
private String surname;
private int age;
private int experience;
private String street;
private int building;
private int local;
private String city;
public Worker() {
}
public Worker(String name, String surname, int age, int experience, String street, int building, int local,
String city) {
setIdentifier();
this.name = name;
this.surname = surname;
this.age = age;
this.experience = experience;
this.street = street;
this.building = building;
this.local = local;
this.city = city;
}
public void setIdentifier() {
this.identifier = (int) (System.currentTimeMillis() / 1000);
}
}
Upvotes: 0
Views: 50
Reputation: 12275
You should use generics to let compiler relax with your types. First make your interface generic:
public interface TypeOfWorker<T extends Worker> {
void addWorker(T worker);
}
Then declare the generic type of your class:
public class OfiiceWorke extends Worker implements TypeOfWorker<OfiiceWorke> {
With these modifications the below is fine:
@Override
public void addWorker(OfiiceWorke worker) {
Upvotes: 2