Java Inheritance Array

I think i understand what you try to do. First of all Department should not extend Employee. That would only make sence, if your apartment was some kind of Employee. Secondly your department array is an array of arrays, because you are using the square brackets 2 times. If you just make it a usual array you can then use it to save all the names of the departments. Now you only need a way to set these departments, and a cosntructor to make an object of the class, and ready is your Department class:

class Department {
    private String[] department;
    public Department() { }

    public String[] getDepartment() {
        return department;

    public void setDepartment(String[] newDepartment) {
        department = newDepartment;

Your Employee class should also get a constructor, so you can make an object of the class:

public Employee() { }

Now you need to make an String Array with all departments in it and pass it to your class. Then you can use the index of these deparments as the departmentId in employee:

String[] departmentNames = new String[6];
Department departmentManager = new Department();
Employee e1 = new Employee();

departmentNames[0] = "xyz";
departmentNames[1] = "asd";



Now the departmentId in employee shows which entry of the array in department is his department and you can show it by calling:


CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top