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

  • Home
  • SEARCH
  • 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 9080137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:00:25+00:00 2026-06-16T20:00:25+00:00

import java.util.*; public class Employee { private int empId; private String name; private int

  • 0
import java.util.*;

public class Employee
{
    private int empId;
    private String name;
    private int age;
    public Employee(String name, int id, int age )
    {
        this.name = name;
        this.empId = id;
        this.age = age;
    }
    public int getId()
    {
        return empId;
    }
    public int getAge()
    {
        return age;
    }
    public String getName()
    {
        return name;
    }

}

class SortById extends Employee implements Comparable<SortById>
{
    public SortById(String name, int id, int age)
    {
        super(name, id, age);
    }
    public int compareTo(SortById other)
    {
        if (this.getId() > other.getId()) return 1;
        if (this.getId() < other.getId()) return -1;
        return 0;

    }
}

class SortByName extends Employee implements Comparable<SortByName>
{
    public SortByName(String name, int id, int age)
    {
        super(name, id, age);
    }

    public int compareTo(SortByName other)
    {
        if (this.getName().compareTo(other.getName()) > 0) return 1;
        if (this.getName().compareTo(other.getName()) < 0) return -1;
        return 0;

    }
}

class Test
{
    public static void main(String args[])
    {
        Employee[] array = new SortById[3];
        array[0] = new SortById("Gautam", 1222, 20);
        array[1] = new SortById("Shivam", 1221, 20);
        array[2] = new SortById("Ankit", 1223, 21);
        System.out.println(array[0] instanceof SortByName);

        Arrays.sort(array);
        for (int i = 0; i < array.length; i++)
            System.out.println("ID: " + array[i].getId() + " Name: " + array[i].getName() + " Age: " + array[i].getAge());
        Employee[] array2 = new SortByName[3];
        array2[0] = new SortByName("Gautam", 1222, 20);
        array2[1] = new SortByName("Shivam", 1221, 20);
        array2[2] = new SortByName("Ankit", 1223, 21);
        Arrays.sort(array2);
        for (int i = 0; i < array2.length; i++)
            System.out.println("ID: " + array2[i].getId() + " Name: " + array2[i].getName() + " Age: " + array2[i].getAge());

    }
}

This program runs fine, I just wanted to ask since I’m using the parameterized version of Comparable, should the reference I pass into the compareTo be of SortById type or SortByName type?

The code runs fine even if the reference is of type Employee, although pointing to its subclass (SortByName or SortById).

How is an implicit cast happening? I have read is not possible i.e., it’s not possible to cast a superclass type to subclass implicitly.

  • 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-16T20:00:27+00:00Added an answer on June 16, 2026 at 8:00 pm

    You never call compareTo which explains why you don’t need the cast. The actual call is within Arrays.sort which takes care of it (by actually using the raw Comparable)

    Also, in such a case the compiler will generate 2 compareTo methods: the one you explicitly define with the SortByXX parameter, and another one with an Object parameter which delegates to the first one.

    As @martijno says it, you will run into problems if you add a simple Employee in your array, which will lead to ClassCastException (from Employee to either Comparable or SortByXX). Same will occur if you mix SortByName and SortById.

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

Sidebar

Related Questions

I have a user defined class, say import java.util.Calendar; public class Employee{ private String
import java.util.Scanner; public class Test { private static int decimalNum = 0; private static
import java.util.*; public class PossibilityGame { private List<String> list1, list2, list3, list4; public PossibilityGame()
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
import java.io.*; import java.util.*; public class Readfilm { public static void main(String[] args) throws
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
Why doesn't this class compile? import java.util.*; public class Caesar { public static void
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class InversionCounter { public static void main(String[]
import java.util.Scanner; public class feetToMeters { public static void main (String [] args) {

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.