Nirmalya Basu
Nirmalya Basu

Reputation: 31

Accessing attributes, methods of different class objects stored inside an object type Arraylist

I have different weapon classes with various attributes and functions (some of which are similar). I am trying to iterate through objects of classes and trying to access those attributes based on certain conditions. Below are some of the objects I created and am storing inside an ArrayList of type Object.

Ak117 ak117 = new Ak117(); 
Ak47 ak47 = new Ak47(); 
Bk57 bk57 = new Bk57();
ArrayList <Object> weaponObjects = new ArrayList<>(Arrays.asList(ak117, ak47, bk57);
int damage = weaponObjects.get(0).damageStats; 
//damage stats is an integer inside AK117 class that returns its damage

When I do this Eclipse can't identify .damageStats; and throws an error. Is there any way wherein I can access all attributes or methods of these objects?

Upvotes: 0

Views: 69

Answers (5)

You can use Interface or Abtract class

Example

  1. Create an interface that contains all the common methods of the classes:
public interface Weapon{
    int getDamageStats();
    void shoot();
    
    // more method
}
  1. Creat class implements this interface
class Ak47 implements Weapon{

    @Override
    public int getDamageStats() {
        return 100;
    }

    @Override
    public void shoot() {
        System.out.println("Ak shoot");
    }
}

class Bk47 implements Weapon{

    @Override
    public int getDamageStats() {
        return 500;
    }

    @Override
    public void shoot() {
        System.out.println("Bk shoot");
    }
}
  1. Call List by Weapon:
Ak47 = new Ak47();
Bk47 = new Bk47();
ArrayList <Weapon> weaponsList = Arrays.asList(ak47, bk47);
int damage = weaponsList .get(0).getDamageStats();

Upvotes: 1

user14008453
user14008453

Reputation: 1

Yep, this is a casual problem with the Object class. If you initiate your ArrayList as an Object, as someone already said, you'll only be able to reach Object Class's methods. By what you sent, we can think that your other classes share communs attributes. So the best idea would be to pass by a SuperClass, which will be extended by every others classes and have a getter on it (in this case, a getDamageStats()). Then you initiate your ArrayList as the SuperClass instead of Object and you'll be able to reach what you seem to want

Upvotes: 0

user12949379
user12949379

Reputation:

I think you should rethink adding inheritance, I am not a gun expert but maybe by country idk. Another option is with abstract class and just throw the methods that are common and modify the others. Then just like Rob Evans said iterate over a list, but my advise is to use new ArrayList<>(new Ak47(), ...etc) and not Arrays.asList.

Upvotes: 0

Rob Evans
Rob Evans

Reputation: 2874

you need something like the below:

ArrayList <Weapon> weaponsList = Arrays.asList(ak117, ak47, bk47);

If you only store the Objects you can only call the Object's methods for each array list entry. If you use Weapon (an interface for all weapons that defines getDamageStats()) all subclasses can then have this method called.

Upvotes: 0

lucasfelber
lucasfelber

Reputation: 73

You need to write a getter in every weapon class.

public class Ak47 {
    int damageStats;
    
    public int getDamageStats() {
        return damageStats;
    }
}

You can access damageStats in another class by creating an instance of Ak47 and calling the getDamgeStats on it.

public class Example {
    Ak47 ak47 = new Ak47();
    
    int example = ak47.getDamageStats();
}

Upvotes: 0

Related Questions