Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8975843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:58:57+00:00 2026-06-15T18:58:57+00:00

I have a small project that I’m working with inheritance and polymorphism. I have

  • 0

I have a small project that I’m working with inheritance and polymorphism. I have an ArrayList of type Employee that contains both Hourly and Salary employee objects. I would like to be able to use a for loop to call a calcPay function in the Hourly class provided the object in the ArrayList of type Employee is an Hourly employee. The line

System.out.println("Wage: " e.calcPay());

Gives the error The method calcPay() is undefined for type employee. How do you downcast the object? I’ve looked in a lot of forums and I couldn’t find an option that would allow me to do it inline or without writing an abstract method that I’d have to include in all of the child classes of Employee. Any insight would be much appreciated.

public class Driver {

     public static void main(String[] args) {

          ArrayList<Employee> list = new ArrayList<Employee>();
          Employee emp1 = new Hourly("Hourly Emp", "123 E Center", "555-555-5555", 00001, "123-45-6789", 12.75);
          Employee emp2 = new Salary("Salary Emp", "123 E Center", "555-555-5555", 00001, "123-45-6789");

          list.add(emp1);
          list.add(emp2);

          for(Employee e : list){
              if(e instanceof Hourly)
              {
                  System.out.println("Wage: " e.calcPay());
              }
           }
    }

    public abstract class Employee {

        private String name, address, phone, ssn;
        private int empNo;

        Employee(String name, String address, String phone, int empNo, String ssn)
        {
            this.name = name;
            this.address = address;
            this.phone = phone;
            this.empNo = empNo;
            this.ssn = ssn;
        }
    }


    public class Hourly extends Employee {

        private double wage;

        Hourly(String name, String address, String phone, int empNo, String ssn, double wage) 
        {
            super(name, address, phone, empNo, ssn);        
                    this.wage = wage;
        }

        public double calcPay(double hours)
        {
                return wage * hours;
        }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:58:58+00:00Added an answer on June 15, 2026 at 6:58 pm

    Even though you are making sure e is of type Hourly, you still need to cast it and use Hourly type to call calcPay() because e is of type Employee and Employee is not aware of any calcPay() method because you have defined calcPay() as only Hourly class method.

              if(e instanceof Hourly)
                  {
                     Hourly hourly = (Hourly)e;   
                      System.out.println("Wage: " hourly.calcPay());
                  }
    

    If you want calcPay() accessible for all Employee instances, you need to define calcPay() as abstract method in Employee class, then you can avoid casting.

    Updated:

     if(e instanceof Hourly)
                  {
                      System.out.println("Wage: " ((Hourly)e).calcPay());
                  }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small project that I will be working on shortly that collects
I have a small project I am working on which is WPF c# that
I am working on a small project that sends SMS Messages. I already have
I have a small project that I want to deploy to both Heroku and
I have a small c++ project I've been working on in XCode, that I'd
i'm working on a small project that might have an additional coder at some
I have a small project that I was using node-dirty for, but it's not
I have a small project and I noticed that I have a lot of
I have a small- to medium-size project that I am doing for my software
I have a small RIA that I built as a learning/make-my-life-easier project that uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.