467,161 Members | 995 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

Calling Subclass Methods from a Driver Class

I'm having issues with calling a method defined in a subclass on a superclass object. This program is an exercise using inheritance, with an Employee superclass, Salaried and Hourly classes that extend the superclass and a Temp class that extends the Hourly class. From what I've read, this shouldn't be happening, since the object in employees[0] is a Salaried object.
The input is a text file with every line being some piece of information, in the following format:

typeOfEmployee - Salaried, Hourly, etc.
SSN
lastName, firstName
addressLine1
addressLine2
filingStatus-exemptions
...employee specific lines...such as hours, totalPay, benefitAdjustment, etc.

nextEmployee
etc.
etc.

nextEmployee
etc.
etc.


I'm using Windows XP SP2 and have the most current JDK.

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*;
  2.  import java.util.regex.*;
  3.  
  4.  public final class Driver {
  5.  
  6.     public static Employee[] employees = new Employee[1024];
  7.  
  8.     public static int        employeeCount;
  9.  
  10.     public static void main( String[] args ) {
  11.  
  12.     Scanner s = new Scanner(System.in);
  13.  
  14.    s.useDelimiter("\r\n");
  15.  
  16.    while (s.hasNext()) {
  17.  
  18.        String line = s.next();
  19.  
  20.         if (line.equals("")) {
  21.         continue;            
  22.  
  23.         } else if (line.equals("SALARIED")) {
  24.  
  25.  
  26.         String[] lines = new String[8];
  27.  
  28.             for (int k = 0; k < 8; k++) {
  29.  
  30.                 lines[k] = s.next();
  31.  
  32.             }
  33.             employees[employeeCount++] = new Salaried(lines);
  34.  
  35.  
  36.         } else if (line.equals("HOURLY")) {
  37.  
  38.         String[] lines = new String[8];
  39.  
  40.             for (int k = 0; k < 8; k++) {
  41.  
  42.                 lines[k] = s.next();
  43.  
  44.             }
  45.             employees[employeeCount++] = new Hourly(lines);
  46.  
  47.  
  48.         } else if (line.equals("TEMP")) {
  49.  
  50.  
  51.  
  52.         String[] lines = new String[8];
  53.  
  54.             for (int k = 0; k < 8; k++) {
  55.  
  56.                 lines[k] = s.next();
  57.  
  58.             }
  59.             employees[employeeCount++] = new Temp(lines);
  60.  
  61.  
  62.  
  63.            }
  64.  
  65.  
  66. } System.out.println(employees[0].getTotalPay());         
  67.     }     
  68. }
  69.  
I know that the first entry in the Employee array is a Salaried object because I wrote a test input, and in the Salaried subclass of Employee there is a public method getTotalPay() that should return the total pay.
I keep getting a "cannot find symbol" error with the symbol being the getTotalPay() method and it says the location is in the Employee class. Any suggestions for a beginning programmer? Look at line 66.
Oct 17 '07 #1
  • viewed: 2756
Share:
3 Replies
8TB
Your array is of type Employee. You can only call methods in the Employee class on its objects. If you want to call a method in another class that extends Employee, you have to cast the Employee object to an object of the subclass type first.
Oct 18 '07 #2
Your array is of type Employee. You can only call methods in the Employee class on its objects. If you want to call a method in another class that extends Employee, you have to cast the Employee object to an object of the subclass type first.
Thank you!
Thank you!
Thank you!

Everything works now!

Isaac B.
Oct 18 '07 #3
8TB
Thank you!
Thank you!
Thank you!

Everything works now!

Isaac B.
Are you trying to cover for all the other posters who don't say "thank you"?

P.S You're welcome.
Oct 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Steven Bethard | last post: by
3 posts views Thread by john | last post: by
4 posts views Thread by Kurt Smith | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.