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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:37:37+00:00 2026-06-11T07:37:37+00:00

I have two objects of classes Person and Employee. Both classes have common attribute

  • 0

I have two objects of classes Person and Employee.
Both classes have common attribute age.
And I have added few objects of both theses classes to an Arraylist and now I want two write one Comparator and pass it to sort method of Collections class.
And want the list to be sorted on the basis of age.
I am trying this just for getting more clarity on use of Comparable and Comparator in Java.

EDIT:
Reason Why I was asking this question was that I was not clear about Comparator and Comparable.
I read somewhere that If Class implements Comparable then it can not be compared against objects of other classes (Because of Class cast exception).

Now if I create relation ship in Employee and Person then there is no need to implement Comparator (Unless I want to do sorting on the basis of name or other common attribute of these classes).

Once Again, I have asked this question to get more clarity on Comparator and Comparable.

Till now, What I have been able to understand is that If I want to do sorting on more than one parameters then I should implement Comparator and pass it to Collections.srot().

Or If I don’t have control over the Class of object being sorted, then custom sorting I should implement Comparator.
(Correct me if I am wrong or I am missing something)

EDIT

I think now I understand the use of Comparable and Comparator.
To make sure I got the right concepts, here is what I understand:
If I want to compare two objects of different classes (and having no relation ) then I should use raw comparator and override the compare method.
Another reason why somebody would implement Comparator instead of Comparable is
to have flexibility in sorting.
And what I understand from natural sorting is the logic implemented by Class in CompareTo method.

Hope I am correct with all these points.

  • 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-11T07:37:39+00:00Added an answer on June 11, 2026 at 7:37 am

    You can define an interface called Ageable and Employee and Person can implement this interface. Also you can define an abstract class (and Employee and Person will be inherited from the abstract class) with the same effect. Note that you can also use a normal class for this reason, you can do whatever you need.

    Also, if there is no reason to say that an Employee is not a Person (for instance robots are working at the office) then Employee can be inherited from Person.

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

Sidebar

Related Questions

I am really confused about object relationships! I have two classes Person and Address.
I have a class that contains objects of two other classes. I need one
Let's say you have two classes, Person and Address. Person has a reference to
I have two classes, Car and Person. Car has as one of its members
I have these two classes: class Student { String name; String age ; }
I have two entity classes: Person and Position . public class Person { public
I have two classes: public class Person { public string FirstName { get {
I have this two classes: class Person(db.Model): person_name = db.StringProperty(required = True) #gender =
Here's the deal: I have two objects of different classes, a DataSizeAction and a
I have a parent entity, Person, and two children entities : Caller and Employee.

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.