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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:06:00+00:00 2026-05-26T19:06:00+00:00

Just doing a quick edit up at the top of my post. The question

  • 0

Just doing a quick edit up at the top of my post.
The question is if i extend a class and both classes have the same variable why is the extended class getting the original classes value.

This has been answered but would be more than happy to read more information about this.
Polymorphism isn’t a strong suit of my so i’m having to relearn this stuff after not using it for months.

So i’m trying to program a game and one of the requirements is some type of list of object that can be scanned in a loop. The trouble starts with my test of ArrayList. I have 3 classes i made to be added in to my array list. Entities, Monster extends Entities, and Character extends Entities. do note Monster and Character both extends Entities class.
I decided that i should make the ArrayList and added new objects directly in to each element(the word element is normally used with array not ArrayList correct?) and use a log function i programmed to note down values of the objects in the ArrayList.
I made a for loop and ran both Monster and Character .getHP() method that they inherited from Entities and the results was the HP from Entities not Monster or Character unless i added a new value to the classes HP using the setHP(int i) method also inherited Entities.

Since my program requires most of the classes and we only need about 99% i decided to make a test project to basically do the above in a smaller form since i don’t want to be copying pasting 20 or more .java files here. Anyways here’s my test classes and results.

    import java.util.ArrayList;

    // class to test interactions between Extended classes
    public class testHP {
        ArrayList<Entities> ent = new ArrayList<Entities>();

        public static void main(String[] args) {

            ///make reference varaible so i don't have to make test() and ent static. 
            testHP hp = new testHP();
            hp.test();
        }
        void test(){
            /// the 3 classes to the arraylist. I could of made a reference variable but didn't.
            this.ent.add(new Character());
            this.ent.add(new Monster());
            this.ent.add(new NPC());
            ///prints out whats in the array list so i know that i have the correct objects.
            System.out.println(this.ent);

            /// this prints out { ent(i) HP "is" i } to tell which class is being called and it's HP at this point.
            for(int i=0; i<ent.size();i=i+1) {System.out.println(this.ent.get(i).getHP() +" is "+ this.ent.get(i));}
            /// this adds i plus 10 then does the same as the last for loop.
            for(int i=0; i<ent.size();i=i+1) {
                this.ent.get(i).gainHP(i+10);
                System.out.println(this.ent.get(i).getHP() +" is "+ this.ent.get(i));}
        }
    }



abstract public class Entities {
    private int HP = 1;

    int getHP(){
        return HP;
    }
    void gainHP(int hp){
        HP = this.HP + hp;
    }
}



///Character is to show what happens when i change the value and make it static.
public class Character extends Entities {
    private static int HP = 4;
}


///monster is to show a changed variable from the super which is entities.
public class Monster extends Entities {
    private int HP = 4;

}

/// NPC is a class that just to have variables from entities class that aren't edited or made static. 
public class NPC extends Entities {

}

here is my results with these files as they are above. I should have put HP next to the number to the left but you get the idea.

[Character@67f1fba0, Monster@3fbefab0, NPC@133c5982]
1 is Character@67f1fba0
1 is Monster@3fbefab0
1 is NPC@133c5982
11 is Character@67f1fba0
12 is Monster@3fbefab0
13 is NPC@133c5982

My test class for the original ArrayList looks like this.

import java.util.ArrayList;
public class AreaMap extends Map {


    String CLASS = "Area Map";///log requires.
    ArrayList<Entities> ent = new ArrayList<Entities>();    
    AreaMap(){
        Log.Logging(CLASS,"Testing arrayList");

        //random text added to the array.
        ent.add(new Character());
        ent.add(new Monster());
        Log.Logging(CLASS, "ent 1 has " +ent+ " in it");
        for(int i=0; i < ent.size();i = i+1){
        Log.Logging(CLASS, "ArrayList " + this.ent.get(i).getHealth() +" i="+i);
        }
        for(int i=0; i < ent.size();i = i+1){
            this.ent.get(i).setHP(10+i);
        Log.Logging(CLASS, "ArrayList " + this.ent.get(i).getHealth() +" i="+i);
        }
    }
}

And here are my result from that.

[Area Map]##[Testing arrayList]
[Area Map]##[ent 1 has [Character@2bd1e730, Monster@61a116c9] in it]
[Area Map]##[ArrayList 0 i=0]
[Area Map]##[ArrayList 0 i=1]
[Area Map]##[ArrayList 10 i=0]
[Area Map]##[ArrayList 11 i=1]

Do note that “Log” is a class i made and the method is “static Logging(String origin, String Action){System.out.println([origin]+”##”+[Action]);”
origin is always the class not that it has to be.

Sorry if this isn’t clear. If you need more information to help me i’m more than willing to answer.

  • 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-26T19:06:01+00:00Added an answer on May 26, 2026 at 7:06 pm

    Basically the problem is that you’re trying to declare extra variables in subclasses as if they can “override” the variable in the superclass. Variables don’t work that way – they’re not polymorphic.

    If you need to give each class a different starting number of hit points, I suggest you create a protected constructor in Entities (which should be renamed, btw – e.g. AbstractEntity) to take the initial value of HP (which should probably be renamed hitPoints). Then each subclass would have a public constructor to call the superconstructor with an appropriate value.

    For example:

    public abstract class AbstractEntity {
        private int hitPoints;
    
        protected AbstractEntity(int initialHitPoints) {
            this.hitPoints = initialHitPoints;
        }
    
        public int getHitPoints(){
            return hitPoints
        }
    
        void gainHitPoints(int amount) {
            hitPoints += amount;
        }
    }
    
    public class Monster extends AbstractEntity {
        public Monster() {
            // Every monster starts off with 4 hit points
            super(4);
        }
    }
    

    Now that’s just a matter of changing the initial state. If you wish the different entities to behave differently, you should override the abstract class methods within the subclasses.

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

Sidebar

Related Questions

I have a quick question regarding memory management that I am not quite sure
Quick edit: As was pointed out below I should be doing a BFS but
Quick question. I have been Googling this all morning, but it's either not there,
I have a question of how to develop a small website which is quick
This should a quick question for some easy rep. I'm doing some PHP Website
I'm just doing a simple graph using Open Flash Chart http://teethgrinder.co.uk/open-flash-chart/ which shows the
I'm basically just doing some tests to figure out a good way to write
At first i was just doing if statements to check the type of the
I'm sure I am just doing something really dumb and not seeing it but
I'm sure I'm just doing something stupid here, but I can't quite figure out

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.