I am currently making a text based adventure in Java for the purposes of using it a test platform, to try out new things I learn from this Java book I'm reading.
I am now trying to declare an instance of a subclass (as the player is scripted to find it).
The parent class is Item
and it has two subclasses: Weapon
and Armour
.
However, no matter which way I try and declare it in, the IDE I'm using (Eclipse) flags the line with the following error:
No enclosing instance of type Item is accessible. Must qualify the allocation with an enclosing instance of type Item (e.g. x.new A() where x is an instance of Item).
When I attempt to declare it like any of the following:
Item machinePistol = new Weapon();
Weapon machinePistol = new Weapon();
Item machinePistol = new Item.Weapon();
Weapon machinePistol = new Item.Weapon();
For reference the item class looks like this:
package JavaAIO;
public class Item
{
public String itemName;
public double itemWeight;
public class Weapon extends Item
{
public double damage;
public double speed;
}
public class Armour extends Item
{
public double dmgResist;
public double attSpdMod;
}
}
So if anyone could tell me how I could properly instantiate a Weapon (so I can set the values of its fields and give it to the player), I would greatly appreciate it.