0

The title is wordy and possibly confusing but I am not sure how to make it better...I want to be able to access values in my array list and print them out.

I have an interface called ThingBagInterface. This ThingBagInterface only has one method, and looks like this:

interface ThingBagInterface {
    public String getType();
}

I now have a class called ThingBag, it's a bag that hold a bunch of different stuff, such as Creatures, Buildings etc.

In my ThingBag class, I have initialized all of my Creatures like this:

public void initCreatures(){
     waterSnake = new Creature("Water Snake", Terrain.SWAMP, false, false, false, false, 1, 0);
    etc...
}

and then I have a function populateBag() that looks like this:

public void populateBag(){
    initCreatures();

    bag.add(bears);
}

My array list definition is in ThingBag and looks like this:

ArrayList<ThingBagInterface> bag = new ArrayList<ThingBagInterface>();

My Creature constructor looks like this:

    public Creature(String n, Terrain startTerrain, boolean flying, boolean magic, boolean charge, boolean ranged, int combat, int o){
        name = n;
        flyingCreature = flying;
        magicCreature = magic;
        canCharge = charge;
        rangedCombat = ranged;
        combatValue = combat;
        owned = o;
    }

I want to print out the name of the bear.

So in main I am doing this:

ThingBag tb = new ThingBag();
tb.populateBag();
for(int i= 0; i<tb.bag.size(); i++){
    System.out.println(i+". "+tb.bag.get(i));
}

Why can I not access the name in my bag? If I wasn't using an interface I would be able to say:

System.out.println(i+". "+tb.bag.get(i).name) 

But I can't now. Any ideas on how I can access that value? I can only access memory addresses now...

3 Answers 3

2

Your bag variable is declared as

ArrayList<ThingBagInterface> bag ...

This, conceptually, means that it contains at least ThingBagInterface objects. It can contain any type of ThingBagInterface, but it has to at least be a ThingBagInterface.

What this also means is that the compiler can only guarantee that it contains ThingBagInterface and therefore you can only interact with its elements as ThingBagInterface instances.

name is not a field that exists on the ThingBagInterface type, it exists on Creature.

You can either cast the returned value of bag.get(i) or declare a getName() method of ThingBagInterface, implement it in the sub types, and invoke it in your loop.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this is my first time trying to use an interface so I am going to make a lot of dumb mistakes until I actually figure it out. Thank you again.
Note that if you're going to cast the elements to Creature, there's not much reason to have a ThingBagInterface at all. ArrayList<Creature> would work better.
@Sarah Take note of what user2357112 just said. Do you have many ThingBagInterface? Do they all have a name? Consider making a base class for all of them.
They do all have a name. I'm unsure if I want an interface or inheritance really. I did have an array list of Creatures, but I needed to change it because my Array List needs to be holding several different types of things. They do all have a name, so maybe that is where I should go, but name is all they have in common.
2

You'll need to design the ThingBagInterface interface more sensibly. Is having a name a requirement? If so, the interface needs a way to access the object's name. (This needs to be a method, as interfaces can't specify fields.) While we're at it, picking a more informative name than ThingBagInterface would be a good idea.

2 Comments

it's an interface for a bag that holds "things" which is the game piece names for the board game I'm trying to implement, so it's pretty informative, but I do get what you are trying to say, so thank you
@Sarah: The name doesn't give any information about how it relates to ThingBag. It sounds like ThingBag is supposed to implement it, rather than the things contained in a bag. Imagine if the interface for passengers in a car was called CarInterface.
1

Maybe do a cast like so System.out.println(i+". "+(((Creature)tb.bag.)get(i));

This is because the interface does not have a name attribute. Remember fields in interfaces are always implicitly public static and final.

I hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.