I am currently creating a text-based Java application for use in a test platform to try new things that I learn from this Java book that I am reading.
Now I am trying to declare an instance of a subclass (as the script script to find it). The parent class is Item and has two subclasses: Weapon and Armour .
However, no matter how I try to declare it, the IDE used (Eclipse) puts a line with the following error:
There is no instance of type Item available. You must assign the selection using an instance of the Item type (for example, x.new A (), where x is an instance of Item).
When I try to declare it as 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 element class is as follows:
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 someone could tell me how I can create a weapon correctly (so that I can set the values โโof its fields and pass it to the player), I would really appreciate it.
java instantiation subclass
James
source share