Calling a subclass method

Hello, I'm a beginner with Java. I have a super class named Student & two subclasses, undergraduate & graduate. I'm trying to call a method from one of those subclasses, but it will only let me call the Student methods and not the subclass methods. I have an array of Students in a seperate class with this code.if(class_room[current_index]instanceof Undergraduate_Student)

{

jTextField4.setText(class_room[current_index].getHighSchool());

}

Undergraduate_Student class has a getHighSchool method, and Graduate Student has a getCollege method.Can I do it this way, or do I have to put the method in the superclass in order for it to work?

[799 byte] By [true_Roba] at [2007-11-26 22:51:35]
# 1
You'll have to cast it to an Undergraduate_Student object first:Undergraduate_Student us = (Undergraduate_Student)class_room[current_index];// now invoke a method specific to Undergraduate_Student// ...
prometheuzza at 2007-7-10 12:13:43 > top of Java-index,Java Essentials,Java Programming...
# 2
You might also want to learn the instanceOf command. Using it you can walk through your students data structure and check is they are Student, Grad Students, etc. That way you'll know what you can and can't do with their particular type object.Bernie
Bernie_Hunta at 2007-7-10 12:13:43 > top of Java-index,Java Essentials,Java Programming...
# 3
> You might also want to learn the instanceOf command. Did you read the code sample they posted?
floundera at 2007-7-10 12:13:43 > top of Java-index,Java Essentials,Java Programming...
# 4

> You'll have to cast it to an Undergraduate_Student

> object first:Undergraduate_Student us =

> (Undergraduate_Student)class_room[current_index];

> // now invoke a method specific to

> Undergraduate_Student

> // ...

Thanks, it works now.

true_Roba at 2007-7-10 12:13:43 > top of Java-index,Java Essentials,Java Programming...