0

Okay so I have an Interface and 2 classes that implement it. The only problem is both classes have methods within them that aren't listed in the interface. I have a test class that uses an ArrayList of the interface type:

ArrayList<Company> employees = new ArrayList<Company>();

The program is designed to allow creation of two seperate classes of employees

public class Salaried implements Company 

and

public class Hourly implements Company

and allow certain methods to be used with them. The two classes both implement the interface, but also have additional methods unique to themselves.

In the test class, I'm trying to use the arrayList to store the different employees that are created, so that their methods can be used later. However the program won't compile as I use methods that aren't in the interface when creating the employees and it won't let me use those methods.

How should I go about fixing this? Is it a mistake within the interface or within the classes themselves?

7
  • you can use default method if java 8 Commented Nov 23, 2016 at 2:40
  • How do you expect to use a method if you don't know which class you're referring to? Commented Nov 23, 2016 at 2:47
  • 1
    @shmosel I do know which class i'm referring to. What my question is asking is how to put two classes that implement a common interface into a singular arrayList if they both need to be accessed from the list in order to use methods that aren't included in the interface. Commented Nov 23, 2016 at 2:51
  • If you know which class it is, use a list of the class instead of the interface. Commented Nov 23, 2016 at 2:52
  • 1
    This indicates a problem with your design. Generally speaking, you should operate at the level where you have a common interface. Providing some specific examples might help us suggest alternatives. Commented Nov 23, 2016 at 3:06

2 Answers 2

1

If I understand correctly, you should cast. Try type type casting to call implementation specific methods.

ArrayList<Company> companies = new ArrayList<>();

    for (Company c : companies) {
        if (c instanceof Salaried) {
            Salaried s = (Salaried) c;
            //call salaried methods
        } else if (c instanceof Hourly) {
            Hourly h = (Hourly) c;
            //call hourly methods
        } else {
            throw new AssertionError("Unknown subtype of Company found.");
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

This will work, but it is a nasty code smell, and there is usually a cleaner way to handle the issue.
@chrylis Agreed. I would always prefer to use the super type. But this answer is specifically to help the OP.
0

However the program won't compile as I use methods that aren't in the interface when creating the employees and it won't let me use those methods.

I'm not sure what you mean by this. If you instantiate a variable of type Salaried, you can call your methods specific to the Salaried class. This is also true for Hourly. From there, you can add to your ArrayList since both Salaried and Hourly implements Company.

2 Comments

This is the part that causes the error: employees.add(new Salaried()); System.out.println("Please enter employee name: "); String _name = in.nextLine(); employees.get(0).setName(_name); System.out.println("Please enter the social security number: "); String _ssn = in.nextLine(); employees.get(0).setSsn(_ssn); System.out.println("Please enter the annual pay: "); double _annualSalary = in.nextDouble(); employees.get(0).setPay(_annualSalary); System.out.println("Salaried Employee has been created."); - It does set name and set ssn fine, but not set pay
If you want to continue using an ArrayList of Company, then instantiate a Salaried employee first, set its properties, and then add to the ArrayList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.