For some reason the following code won't work when trying to create a object from different subclasses based on the result of an if-else statement:
if (option == 1) {
UndergradTA student = new UndergradTA();
student.setIsUnderGrad(true);
} else if (option == 2) {
GradTA student = new GradTA();
student.setIsGrad(true);
}
When I then try to use methods on the "student" class later on in the main method it won't allow me, saying "student cannot be resolved".
Change Your Code to:
UndergradTA student = null;
GradTA stud = null;
if (option == 1) {
student = new UndergradTA();
student.setIsUnderGrad(true);
} else if (option == 2) {
stud = new GradTA();
stud.setIsGrad(true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With