473,383 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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
3 3483
r035198x
13,262 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
r035198x
13,262 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

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

Similar topics

3
by: Danny Shevitz | last post by:
Howdy, I am trying to call class methods that have been created via a "type" function. I have enclosed a simplified example that shows what I am trying to do. In particular I am calling from the...
6
by: Steven Bethard | last post by:
So when I'm writing a class and I define an __init__ method, I sometimes haven't called object.__init__, e.g.: class C(object): def __init__(self, x): self.x = x instead of class...
3
by: john | last post by:
I have a situation where i have a base class and a sub-class. A null instance of the sub-class is passed into a function. The function needs to create a new instance of the sub-class, but the...
5
by: Keith Patrick | last post by:
Could someone tell me if it's possible (and if so, how) to call an explicitly-implemented interface method from a subclass? I have a class in which I have to explicity implement some methods, but...
4
by: Claire | last post by:
I'm having real brain failure today. I've done this lots of times with constructors but not with virtual methods and the compiler complains because Ive put the :base(foo) after the function...
8
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library...
13
by: TS | last post by:
Say i have a class car with properties: Color, Make, Model, Year, DriverID And a Driver class with properties: DriverID, Name The driverID PRIVATE property is the id of the driver from say a...
31
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i...
4
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.