JetBrains Academy: Inheritance - Kamil-Jankowski/Learning-JAVA GitHub Wiki
JetBrains Academy: Inheritance
Employees:
Develop a class hierarchy of employees. The hierarchy should include three classes:
- Employee is the base class. It includes three fields (name, email and experience), one constructor with three arguments and three getters: getName(), getEmail(), getExperience().
- Developer is a subclass. It includes fields from the base class and two additional fields (mainLanguage, skills), one constructor with five arguments and two getters: getMainLanguage(), getSkills().
- DataAnalyst is another subclass. It includes fields from the base class and two additional fields (phd, methods), one constructor with five arguments and two getters: isPhD(), getMethods().
You need to define types of the fields and write suitable constructors. To understand it see the code below.
String[] skills = { "git", "Scala", "JBoss", "UML" };
Developer developer = new Developer("Mary", "[email protected]", 3, "Java", skills);
String[] mlMethods = { "neural networks", "decision tree", "bayesian algorithms" };
DataAnalyst analyst = new DataAnalyst("John", "[email protected]", 2, true, mlMethods);
It should work correctly with your class hierarchy. Do not forget to write getters with the specified name (otherwise the test system won't be able to check your solution).
class Employee {
// fields
protected String name;
protected String email;
protected int experience;
// constructor
public Employee(String name, String email, int exp){
this.name = name;
this.email = email;
this.experience = exp;
}
// methods
public String getName(){
return name;
}
public String getEmail(){
return email;
}
public int getExperience(){
return experience;
}
}
class Developer extends Employee {
// fields
private String mainLanguage;
private String[] skills;
// constructor
public Developer(String name, String email, int exp, String mainLanguage, String[] skills){
super (name, email, exp);
this.mainLanguage = mainLanguage;
this.skills = skills;
}
// methods
public String getMainLanguage(){
return mainLanguage;
}
public String[] getSkills(){
return skills;
}
}
class DataAnalyst extends Employee {
// fields
private boolean phd;
private String[] methods;
// constructor
public DataAnalyst(String name, String email, int exp, boolean phd, String[] methods){
super (name, email, exp);
this.phd = phd;
this.methods = methods;
}
// methods
public boolean isPhD(){
return phd;
}
public String[] getMethods(){
return methods;
}
}