1

So I want to use a method to write multiple objects to respective files. However I do not know how to import the array of Objects without defining the specific Object.

The people is class is purely for storing the created objects in arrays so it is easier to access across other classes.

For example

public class People {

    private Student[10];
    private Teacher[10];

    public void setStudentArray(Student, index) {
        Student[index] = Student;
    }

    public void setTeacherArray(Teacher, index) {
        Teacher[index] = Teacher;
    }
}

public class Student extends People {
    String name;
    int StudentID;
    public String getName() {
        return name;
    }
}

public class Teacher extends People {
    String name ;
    int Teacher ID;
    public String getName() {
        return name;
    }
}
public class Main {
    People p = new People();

    public void main (String[] args) {
        Student s = new Student("default-name" , 1);
        p.setStudentArray(s, 0);

        Teacher t = new Teacher("default-name", 1);
        p.setTeacherArray(t, 0);

        outputName(p.getStudentArray, 0);
        outputName(p.getTeacherArray, 0)
    }

    //THIS IS WHERE I AM STRUGGLING I dont know how to pass teachers or students array to it.
    //I want the Object[] parameter to accept both Student[] and Teacher[]

    public void outputName(Object[], index) {
        System.out.println(Object[index].getName);  
    }   
}

I think that my Method taking an Object[] is wrong but I do not know how to approach it otherwise. I believe the issue is that Object[] is an entirely different class to Teacher[] and Student[] and this is where I am going wrong.

I want to use the .getName method in both the classes of Teacher and Student in order to print the name of the Teacher of Student. (Merely so I can see the passing is working.)

If this is just not possible I guess I will just not try a method that can take different objects.

I know that I can just use two methods one for students and one for teachers but I want the method to work for multiple objects so that I can add more object arrays to it.

  • It should be of type `People[]` to facilitate polymorphism, and give it an identifier – Andrew Li Oct 18 '18 at 03:26
  • Ahh I see. Sorry but what do you mean by identifier? – VitriolicAU Oct 18 '18 at 03:28
  • In Java arrays are objects, as you can see in this answer: https://stackoverflow.com/questions/8781022/is-an-array-an-object-in-java You should set the array on the People instance you created: p.setStudentArray(s, 0)., and the same for the teacher array. – Tomaz Fernandes Oct 18 '18 at 03:32
  • Ohh sorry I didnt mean to leave the p off setStudentArray(s, 0) that was a mistake on my part. Thanks for pointing that at. I have changed that now. However I still am unsure of how to pass that array to outputName method. – VitriolicAU Oct 18 '18 at 03:37

3 Answers3

0

So People class is extended by both Student and Teacher.

What commonalities are here?

  1. String name is present in both Student and Teacher
  2. public String getName() is also present in both Student and Teacher

You can move these commonalities to People class. Also ensure to remove the name attribute and getName from Student and Teacher class

So your People updated class can be:

public class People {
    private String name; //Newly added

    private Student[10];   //This ideally shouldn't be in People class rather a different class
    private Teacher[10];   //This ideally shouldn't be in People class rather a different class

    public void setStudentArray(Student, index) {
      Student[index] = Student;
    }

    public void setTeacherArray(Teacher, index) {
      Teacher[index] = Teacher;
    }

    public String getName() {
      return name;
    }

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

The outputname method should be like:

public void outputName(People[] people, index) {
    System.out.println(people[index].getName());  
}

NOTE: I am not correcting the syntax here, but just giving an idea.

Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36
0

What @Li357 said is right... You have to change your modeling a bit. Even if you managed to pass Student[] as an Object[], you wouldn’t be able to call the getName method as it’s not an Object method.

So a better modeling would be to make the getName method a People method, and both Student and Teacher classes would inherit it.

Then you could receive People[] as the outputName method argument, and use the getName method inside.

Tomaz Fernandes
  • 2,429
  • 2
  • 14
  • 18
0

First of all learn how to declare array and choose valid variables.

In your People class do following modifications.

public class People {
    //Declare arrays like this.
    private Student[] student;
    private Teacher[] teacher;

    //Initialize arrays
    public People(){
        student = new Student[10];
        teacher = new Teacher[10];
    }
    public void setStudentArray(Student s,int index) {
        student[index] = s;
    }

    public void setTeacherArray(Teacher t, int index) {
        teacher[index] = t;
    }

    //Add getter methods
    public Student[] getStudentArray(){
        return student;
    }

     public Teacher[] getTeacherArray(){
        return teacher;
    }
}

Inside sub classes Student and Teacher add Argument constructor

Finally in your outputName method you can do something like this.

 public static void outputName(Object[] obj, int index) {

    if(obj instanceof Student[]){
        Student[] s = (Student[])obj;//parsing to student array
        System.out.println("Student name : "+s[index].getName());
    }

    if(obj instanceof Teacher[]){
        Teacher[] teacher = (Teacher[])obj;//parsing to teacher array
        System.out.println("Teacher name : "+teacher[index].getName());
    }


}  

Output:

Student name : default-name
Teacher name : default-name
Exteam
  • 63
  • 7