1

Working on a school assignment, I have an abstract class Person, an abstract class Student which extends Person, and a regular class CollegeStudent which extends student. CollegeStudent reads from a file and sets all the variables (including those which belong to the abstract superclasses). All this works fine.

However, we are supposed to have constructors in the abstract classes, which complicates things because those classes are specifically not supposed to be able to read input on their own. So now I have a situation in which I (presumably) am supposed to use constructors to set the data fields in their respective classes, but of course I need to read input first! The obvious problem here is that the superconstructor call must be the first thing in the constructor of CollegeStudent. This is the problem.

I can have everything run as is and then have another constructor method which can takes all the needed fields as parameters. But I don't see what the point of that is--why have the Driver create the CollegeStudentobject and then create it again (using the ariables from the first object) just for the sake of using the constructor?

Other than that I don't know how this could be done, and this option is odd to me--I don't see what the point is.

Thanks in advance...

Jo.P
  • 1,029
  • 4
  • 13
  • 31

1 Answers1

4

OK, let me elaborate on my comment. A constructor shouldn't read a file to find its own parameters. That's not its job. If I understand correctly, your problem is that you need to have

public CollegeStudent(File f) {
    // get a, b and c from the file, but how?
    super(a, b, c);
}

But a, b and c come from a file. Since super must be the first instruction of the constructor, you can't read the file before invoking super, so you're doomed.

You should use a factory method instead:

public CollegeStudent(int a, int b, int c) {
    super(a, b, c);
}

public static CollegeStudent createFromFile(File f) throws IOException {
    int a;
    int b;
    int c;
    // TODO: read the file and initialize a, b and c
    return new Student(a, b, c);
}

The callers, to construct a CollegeStudent from a file, would simply call

CollegeStudent cs = CollegeStudent.createFromFile(file);

This is a typical OO pattern, which is often used. See Integer.valueOf(), for example.

JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174
  • I think I'm understanding: basically the driver would call your second method to create the class. But...then what purpose does the first constructor serve? – Jo.P Nov 05 '12 at 23:25
  • oh, I see...didn't notice that @ the end...thanks! Got it now! – Jo.P Nov 05 '12 at 23:30
  • Would it be correct to say that everything could just as well be initialized from the createFromFile method, and that the only reason we are using that constructor at all is for the sake of OO? I just want to make sure I get this properly... – Jo.P Nov 05 '12 at 23:38
  • You need a constructor to construct an instance o a class. Thre's no way around it. – JB Nizet Nov 06 '12 at 07:49