Reputation: 45
having hard time calling a method in the main. keeps saying that it cannot find symbol - method addSales(double)
i am doing a project for programming because we just learnt about inheritance and it has 3 super classes and 3 sub classes. i am having a problem with one sub and super class. they are named Hourly and Commission. commission extends hourly. i feel like i have written the method called addSales correctly however when i call it in the main it says that it cannot find the method. am i missing something here? any help would be greatly appreciated.
Commission class:
public class Commission extends Hourly
{
private double totalSales, commission;
public Commission(String eName, String eAddress, String ePhone,
String socSecNumber, double rate, double commissionRate)
{
super(eName, eAddress, ePhone, socSecNumber, rate);
totalSales = 0.0;
commission = commissionRate;
}
public double pay()
{
double payment = super.pay();
payment = (payment + (commission * totalSales));
return payment;
}
public String toString()
{
String result = super.toString();
result += "Total Sales: " + totalSales;
return result;
}
public void addSales(double totalS)
{
totalSales = totalSales + totalS;
}
}
Hourly class:
public class Hourly extends Employee
{
private int hoursWorked;
//-----------------------------------------------------------------
// Sets up this hourly employee using the specified information.
//-----------------------------------------------------------------
public Hourly (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
hoursWorked = 0;
}
//-----------------------------------------------------------------
// Adds the specified number of hours to this employee's
// accumulated hours.
//-----------------------------------------------------------------
public void addHours (int moreHours)
{
hoursWorked += moreHours;
}
//-----------------------------------------------------------------
// Computes and returns the pay for this hourly employee.
//-----------------------------------------------------------------
public double pay()
{
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
//-----------------------------------------------------------------
// Returns information about this hourly employee as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = super.toString();
result += "\nCurrent hours: " + hoursWorked;
return result;
}
}
main:
public class Firm
{
//--------------------------------------------------------------
// Creates a staff of employees for a firm and pays them.
//--------------------------------------------------------------
public static void main (String[] args)
{
Staff personnel = new Staff(8);
Executive staff0 = new Executive ("Sam", "123 Main Line", "555-0469", "123-45- 6789", 2423.07);
StaffMember staff1 = new Employee ("Carla", "456 Off Line", "555-0101", "987-65-4321", 1246.15);
StaffMember staff2 = new Employee ("Woody", "789 Off Rocker", "555-0000", "010-20-3040", 1169.23);
Hourly staff3 = new Hourly ("Diane", "678 Fifth Ave.", "555-0690", "958-47-3625", 10.55);
Hourly staff6 = new Commission("Marcus Gendron", "66 Highland St.", "272-9555", "123-62-5678", 6.25, 0.20);
Hourly staff7 = new Commission("Joe Dangerous", "55 dude Court", "555-1267", "777-66-5555", 9.75, 0.15);
StaffMember staff4 = new Volunteer ("Norm", "987 Suds Blvd.", "555-8374") ;
StaffMember staff5 = new Volunteer ("Cliff", "321 Duds Lane", "555-7282");
personnel.addStaff(staff0);
personnel.addStaff(staff1);
personnel.addStaff(staff2);
personnel.addStaff(staff3);
personnel.addStaff(staff4);
personnel.addStaff(staff5);
personnel.addStaff(staff6);
personnel.addStaff(staff7);
staff6.addHours(35);
staff6.addSales(400.0);
//error is being shown here ^^^^
staff7.addHours(40);
staff7.addSales(950.00);
staff0.awardBonus (500.00);
staff3.addHours (40);
personnel.payday();
}
}
Upvotes: 0
Views: 223
Reputation: 4539
Here You can only able to call methods that are present on Hourly class.
Because You are referencing Hourly class now as addSales in not present on Hourly class it is not accessible.
Hourly staff6 = new Commission("Marcus Gendron", "66 Highland St.", "272-9555", "123-62-5678", 6.25, 0.20);
In your above code Commission class object have reference to Hourly class. So Commission object will only allowed to access those methods which are Hourly class ( because they are inherited ) and overridden methods of hourly class in commission class.
Upvotes: 0
Reputation: 3649
I see the object creation below:
Hourly staff6 = new Commission("Marcus Gendron", "66 Highland St.", "272-9555", "123-62-5678", 6.25, 0.20);
addSales method is in the child class Commission. But the type of staff6 is Hourly which is the parent class and Hourly class doesnt have this method. This you dont see the method addsales.
For more detailed understanding, try the following line (which will compile fine). And see what all methods you get
Object staff6 = new Commission("Marcus Gendron", "66 Highland St.", "272-9555", "123-62-5678", 6.25, 0.20);
Upvotes: 0
Reputation: 106430
Both staff6
and staff7
are instances of Hourly
. The Hourly
class does not have that particular method attached to it.
You would have to declare them as concrete instances of Commission
instead.
Upvotes: 1
Reputation: 3577
staff6
is declared as Hourly
, and even though it is in fact a Commission
, you are trying to access the super class' method.
Declare it as Commission
.
Upvotes: 0