Using polymorphism?

Employee class

public class Employee {
protected String name;
protected String jobsheetnumber;

public Employee(String n,String j){
this.name = n;
this.jobsheetnumber = j;
}
public Employee(String name)
{
this.name = name;
}

public String getName() {
    return name;
}

public String getJobsheetnumber() {
    return jobsheetnumber;
}

public void setName(String name) {
    this.name = name;
}

public void setJobsheetnumber(String jobsheetnumber) {
    this.jobsheetnumber = jobsheetnumber;
}



}

Mechanical class

public class Mechanic extends Employee{


public Mechanic(String name,String jobsheetnumber){
super(name,jobsheetnumber);
}
}

Supervisor Class

public class Supervisor extends Employee{


public Supervisor(String name){
super(name);
}
}

Company class [fragment]

public class Company {
private String companyname;
private String companyaddress;
private String postalcode;
private String city;
private String country;
private String telephonenumber;
private String faxnumber;
private String province;
private Employee supervisor;
private Employee mechanic;




public Company(String companyname,String companyaddress,String postalcode,String city,String country,String telephonenumber,String faxnumber,String province,String supervisorname,String jobsheetnumber,String mechanicname)
{
 this.companyname = companyname;
 this.companyaddress=companyaddress;
 this.postalcode = postalcode;
 this.city=city;
 this.country=country;
 this.telephonenumber=telephonenumber;
this.faxnumber=faxnumber;
this.province=province;
supervisor = new Supervisor(supervisorname);
mechanic = new Mechanic(mechanicname,jobsheetnumber);




}

The Employee Class is a superclass of the Mechanic and Supervisor classes. Now I use the attributes Employee ie name and jobsheetnumber in subclasses of the Mechanic and Supervisor classes

the code is working fine .. but what if I want to add advanced functionality to Mechanic and Supervisor? then I can’t access these variables because there is a reference to an object of type Employee.

Is this the correct use of polymorphism? what should we use super () as a constructor every time we create a Supervisor / Mechanical object link? Can't we use the extended functionality inside the Supervisor and Mechanic classes?

+4
3

non-zero-arg , , . , .

, , , , , . , , , ; . , , - .

( , , . , Role Employee.)

+3

() . , .

, , .

public class Employee{
    public void work(int hours){ doNothing();}      
}
public class Supervisor extends Employee{
    private Object pen; 
    private Object note;
    @Override
    public void work(int hours){
        observations = superviseWorkers();
        note.write(observations, pen);

    } 

}
public class Mechanic extends Employee{
    private Tool tool;
    private TaskBoard taskBoard;
    @Override
    public void work(int hours){ 
        task = taskBoard.getPendent()
        if(task.canSolveWithTool(tool))
        {
            solveTask(task, tool)
        }
    } 
}

:

employees = new List<Employee>();
employees.add(new Supervisor("foo"));
employees.add(new Mechanic("bar"));

foreach(employee in employees){
   //you don't need to know which kind of employee you are treating here because you are only calling a behavior that all employees have.
   employee.work(8);
}

, , , .

, , , , .

+1

.

1: ( )

  • Employee < ", .
  • Employee . , doWork(), Employee.
  • Mechanic, Supvervisor .

    public interface ISupervise{
         public void doSupervise();
    }
    
    public class Supervisor extends Employee implements ISupervise{
        public void doSupervise(){
    
        }
    } 
    
    public interface IMechanic{
        public void doMechanicWork();
    }
    
    public class Mechanic extends Employee implements IMechanic{
        public void doMechanicWork(){
    
        }
    }
    

2: ( )

Decorator Employee . Mechanic Supervisor Employee. . Decorator.

Sample code can be found @

When to use a decorator template?

+1
source

Source: https://habr.com/ru/post/1623188/


All Articles