penu
penu

Reputation: 1040

How to access child variables from a parent class?

I have 3 classes, Person, Student, and Teacher. Student and Teacher extend Person. How can I access instance variables from within Person? Like if Student and Teacher both have an internal list of names, how can I access both name arrays from within the parent class?

class Person
{
   private String name;
   public void setName(String name) {
       this.name = name;
   }
   public String getName() {
        return this.name;
   }
    public void printAllNames() { //<-- How do you do this?
        for(Student s : students) //can't access students array
           System.out.println(s.getName());
        for(Teacher t : teachers)
           System.out.println(t.getName());
    }
}

class Student extends Person
{
   protected Student[] students;
   Student(String name)
   {
      this.name = name;
      students[students.length] = this;
   }
}

class Teacher extends Person
{
   protected Teacher[] teachers;
   Teacher(String name)
   {
      this.name = name;
      teacher[teachers.length] = this;
   }
}

Upvotes: 2

Views: 1623

Answers (3)

ekcr1
ekcr1

Reputation: 4514

Ignoring the issues with the design and the use of arrays as others have mentioned, if you want to print the names depending on whether it's a student and teacher, you should make printAllNames abstract and have the descendant classes provide their own print implementation. The base class should not know about specifics of properties or methods in subclasses.

class Person
{
    ...

    public abstract void printAllNames();
}


class Student extends Person
{
    ...

    public void printAllNames() {
        for (Student s : students)
            System.out.println(s.getName());
    }
}

class Teacher extends Person
{
    ...

    public void printAllNames() {
        for (Teacher t : teachers)
            System.out.println(t.getName());
    }
}

Upvotes: 1

Maarten Bodewes
Maarten Bodewes

Reputation: 94078

You class design is incorrect. Your Student class should not contain a field students as other students are not part of a Student instance (i.e. composition should not be used here). Instead you should create lists of students outside of these classes if they are required. Same for teachers of course.

A student could have a list of friends, but such relations are better stored outside the class itself as well. The list of friends does is not part of a student. What does need to be part of student depends on what you are trying to model (an address could be part of student if that helps to define student for administration purposes).

If you want to access fields of a higher level class you could downcast to Student from Person but again, that would normally be considered as a technical solution for a design problem.

Upvotes: 1

Prashant
Prashant

Reputation: 2614

Your code is having issue in array.length() because there is no any method like length(). you can access that by array.length.

second there is no name variable in your Student class and you made name as private field in Person Class change that to public or protected.

third you can pass argument in your method like

public void printAllNames(Student[] students, Teacher[] teachers)

and check once.

Upvotes: 0

Related Questions