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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:50:20+00:00 2026-05-27T19:50:20+00:00

That is, I have a list of class ‘ Entity ‘, which is an

  • 0

That is, I have a list of class ‘Entity‘, which is an abstract class containing several different classes.
In the list, I can determine which are of a specific descendant classes using

if (list[i] instanceof Enemy)

However, I cannot, using the list[i] reference, reference fields which are specific to Enemy and not inherited.

Is there any simple way of grabbing a reference to it which will let me reference specific fields?

Otherwise I’ll just rewrite that structure of the code.

Solved: Explicitly casting:

(Enemy)(list[i])

  • 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-27T19:50:21+00:00Added an answer on May 27, 2026 at 7:50 pm

    You can cast the element to the appropriate class.

    Enemy enemy = (Enemy)element; 
    int hitPoints = enemy.getHitPoints(); 
    

    However, when working with a mixed collection of a base type, it is generally because you’re only interested in the polymorphic behaviors, so I encourage you to follow through on at least exploring that particular area of your code. (On that note, it is also generally discouraged to access member fields or variables directly, as your question implies you wish to do, as that violates encapsulation.)

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

Sidebar

Related Questions

Basically I have a custom List class that contains different fruits. Assume that each
I have a list of class items List<MyClass> I have a seperate object that
So let's assume I have a class named ABC that will have a list
I have code that looks like this: template<class T> class list { public: class
I have a list of images that i’ve loaded with the Loader class, but
Given that I have the following WCF service: class LookUpService { public List<County> GetCounties(string
I have an ASP.NET WebService that returns an object of List public class Students
I have a method I'm writing that uses reflection to list a class's static
Suppose class B extends class A . I have a List<A> that I happen
I have a method that takes a list of entities ( Class es) and

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.