Calling Subclass Methods from a Driver Class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bassman2112
    New Member
    • Oct 2007
    • 2

    #1

    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, benefitAdjustme nt, etc.

    nextEmployee
    etc.
    etc.

    nextEmployee
    etc.
    etc.


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

    Code:
     import java.util.*;
     import java.util.regex.*;
    
     public final class Driver {
    
        public static Employee[] employees = new Employee[1024];
    
        public static int        employeeCount;
    
        public static void main( String[] args ) {
    	
        Scanner s = new Scanner(System.in);
    	
       s.useDelimiter("\r\n");
    
       while (s.hasNext()) {
    
    	   String line = s.next();
    
    	    if (line.equals("")) {
    		continue;            
    	
    	    } else if (line.equals("SALARIED")) {
    
    
    		String[] lines = new String[8];
    
    		    for (int k = 0; k < 8; k++) {
     
    		        lines[k] = s.next();
    
    		    }
    			employees[employeeCount++] = new Salaried(lines);
    		    
    
    	    } else if (line.equals("HOURLY")) {
    
    		String[] lines = new String[8];
    
    		    for (int k = 0; k < 8; k++) {
     
    		        lines[k] = s.next();
    
    		    }
    			employees[employeeCount++] = new Hourly(lines);
    		    
    
    	    } else if (line.equals("TEMP")) {
    
    
    
    		String[] lines = new String[8];
    
    		    for (int k = 0; k < 8; k++) {
    
    		        lines[k] = s.next();
    
    		    }
    			employees[employeeCount++] = new Temp(lines);
    			
    						    
    
    		   }
    
            
    } System.out.println(employees[0].getTotalPay());         
        }     
    }
    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.
    Last edited by bassman2112; Oct 17 '07, 11:08 PM. Reason: Forgot to put in the [\CODE]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #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.

    Comment

    • bassman2112
      New Member
      • Oct 2007
      • 2

      #3
      Originally posted by r035198x
      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.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by bassman2112
        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.

        Comment

        Working...