0

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?

3
  • Sorry, but we need to see your classes to help. Maybe interface or abstract class will work in this way Commented Jul 28, 2020 at 8:38
  • Thanks for replying, I have a lot of classes, is there a way I can connect personally to get help, don't think I can post all class details here. Commented Jul 28, 2020 at 8:48
  • I gave an answer @NirmalyaBasu Commented Jul 28, 2020 at 9:03

5 Answers 5

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Although I accepted another answer as his was the first, but details like these help beginner programmers a lot! Kudos for the answer! :)
0

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.

Comments

0

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();
}

1 Comment

Thanks for answering but this doesn't solve my problem, as I can access ak117.damageStats directly, but not when its stored inside an ArrayList as an item.
0

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.

Comments

0

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

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.