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 7913845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:56:16+00:00 2026-06-03T13:56:16+00:00

I have an interface (say Employee) which has 2 classes that implements it (say

  • 0

I have an interface (say Employee) which has 2 classes that implements it (say Assistant and Manager). I also have an interface which has a method that returns a list of the first interface type (say EmployeeLogic interface which returns List. There are two classes implementing this interface (say AssistantLogic and ManagerLogic). In these two classes, the List contains only Assistants (in the AssitantLogic) and only Manager (in the ManagerLogic).

The problem is that I want to sort these two lists based on the methods that exist only in the subclasses. How do I write such a comparator?

Probably the code is better understandable:

public interface Employee {
    public int employeeMethod();
}

public class Manager implements Employee {
    public int employeeMehod(){//[...]}
    public int managerMethod(){//[...]}
}

public class Assistant implements Employee {
    public int employeeMehod(){//[...]}
    public String assistantMehod(){//[...]}
}

public interface EmployeeLogic() {
    public List<Employee> getEmployees();
}

public class ManagerLogic implements EmployeeLogic {
    public List<Employee> getEmployees() {

        List<Employee> employees = [...] //or List<? extends Employee> ?
        [...]
        Collections.sort(employees, new Comparator<Manager>() {
            @Override
            public int compare(Manager o1, Manager o2) {
                return o1.managerMethod().compareTo(o2.managerMethod());
            }
        });
        return employees;
    }

}

public class AssistantLogic implements EmployeeLogic {
    public List<Employee> getEmployees() {

        List<Employee> employees = [...] //or List<? extends Employee> ?
        [...]
        Collections.sort(employees, new Comparator<Assistant>() {
            @Override
            public int compare(Assistant o1, Assistant o2) {
                return o1.assistantMehod().compareTo(o2.assistantMehod());
            }
        });
    return employees;
    }
}

Is there a way to write such a comparator?

I know a workaround (see bellow) but is the above thing possible with only one list?

public class ManagerLogic implements EmployeeLogic {
        public List<Employee> getEmployees() {

            List<Manager> managers = new ArrayList<Manager>()
            [...]
            Collections.sort(managers, new Comparator<Manager>() {
                @Override
                public int compare(Manager o1, Manager o2) {
                    return o1.managerMethod().compareTo(o2.managerMethod());
                }
            });
            List<Employee> employees = new ArrayList<Employee>();
            employees.addAll(managers);
            return employees;
        }

    }
  • 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-03T13:56:17+00:00Added an answer on June 3, 2026 at 1:56 pm

    If I understand you correctly, your problem isn’t about trying to sort a mix of Assistants and Managers, but about the fact that you can only sort either a list of Assistants or a list of Managers, but you want to return a list of Employees.

    In that case, one thing you could do is have getEmployees() return a List<? extends Employee>.
    If that is not an option, and if you don’t intend for the returned list to be modified, you can do return Collections.unmodifiableList(managers).
    If you intend the returned list to be modifiable, then your solution is the cleanest way to do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an interface that has as property another interface. Something like: interface
I have an Interface lets say ISendOut which I've inherited two different classes from
I have class method that returns a list of employees that I can iterate
Let's say we have interface window_creator that responsible for creation of windows. For simplicity
Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1>
lets say I have an NSWindow Class, that has several events for mouse and
Lets say we have this interface: interface IVehicle { ... } And some classes
Say that I have this interface: interface Drawable { Vector2 DrawPosition { get; }
Say I have interface IFoo { /// <summary> /// Comments about Bar method goes
Lets say you have interface definition. That interface can be Operation . Then you

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.