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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:01:10+00:00 2026-05-17T21:01:10+00:00

I have a base Entity class: public class Entity { abstract int getTypeID(); }

  • 0

I have a base Entity class:

public class Entity
{
    abstract int getTypeID();
}

The method int getTypeID() returns a number that is unique to that class:

public class OneEntity extends Entity
{
    int getTypeID()
    {
        return 1; // Actually defined in a constants class
    }
}

Now I want to be able to safely cast it and assign it.

I can do it this way:

public void castTheEntityAndDoSomething(Entity ent)
{
    if (isType(ent, 1)) // this is the 1 from the OneEntity class
    {
        OneEntity oneEnt = (OneEntity)ent;

        // ... and then do something
    }
}

public bool isType(Entity ent, int type)
{
    return ent.getTypeID() == type;
}

But what I’d like to do is combine the cast and the type check in one line.

Something like:

if (OneEntity oneEnt = entityCast(ent, 1))
{
    // use a method specific to the OneEntity class
}

Is this possible? What does the method look like?
It would be even better if I could use the subclass name as an argument of the safe casting method.

EDIT:

So I’ve written this method to avoid the type id etc. etc.

@SuppressWarnings("unchecked")
public static <T extends Entity> T castEntity(Entity ent)
{
    if (ent instanceof T)
    {
        return (T)ent;
    }

    return null;
}

But it has this error:

Cannot perform instanceof check against type parameter T. Use instead its erasure Entity since generic type information will be erased at runtime

What do I need to do to fix this compile error? Is this even going in the right direction?

  • 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-05-17T21:01:11+00:00Added an answer on May 17, 2026 at 9:01 pm

    In case you have defined this typeId for the sole purpose of casting, then please don’t do that and use instanceof operator instead.

    Then you just need to do something like this,

    if (ent instanceof EntityOne) {
       OneEntity oneEntity = (OneEntity) ent;
       // do whatever
    }
    

    Now, taking your example here,

    if (Entity oneEnt = entityCast(ent, 1))
    {
        // do something with oneEnt
    }
    

    Suppose this if statement of yours work and your entityCast() method return OneEntity. Don’t you think you are again getting that returned value in type Entity. Hence spoiling the whole purpose.

    So, please explain what are you trying to do exactly.

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

Sidebar

Related Questions

I have an abstract entity base class defined like this: public abstract class SessionItem
I have the following entity base class marked as serializable: [Serializable] public abstract class
Imagine that I have a generic base class like this: public abstract class AnimalDTO<TA,
In the project I'm working on now, we have base Entity class that looks
I have a base class: class motorcycle { public: virtual int speed() { return
@Entity @Table(name = parent) public final class Parent extends Base { @OneToOne(cascade = CascadeType.PERSIST,
Let's say I have a base class named Entity . In that class, I
I have an abstract base class which inherits Sharp Arch's Entity class: /// <summary>
I have a base class for all my entity types which is like public
In my application I have a mapping like this @MappedSuperclass public abstract class Base

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.