Hello I am trying to create a class that called Lootable and create subclasses such as trunk, locker, etc. that will contain items that can be looted, they will inherit their lootability from the Lootable class. I am thinking of using the ArrayList for this. I have thought of different ways to go about this and have written some code but I am having some difficulties. This is for and assignment and my app idea goes somewhat beyond the requirements of the lesson and no I do not expect someone to due this for me I would however like some help understanding the concepts. Here is the code I have so far:
package TaskTwo;
import java.util.ArrayList;
/**
*
* @author Steven Richardson
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Lootable lt = new Lootable();
Trunk tr = new Trunk();
Trunk trunkTwo = new Trunk("new stuff worth $");
// System.out.println(lt.toString());
// System.out.println(lt.contents);
//
// System.out.println(tr.toString());
// System.out.println(tr.contents);
//
// System.out.println(trunkTwo.toString());
// System.out.println(trunkTwo.contents);
ArrayList <Lootable> lootContainers = new ArrayList<>();
lootContainers.add(lt);
lootContainers.add(tr);
lootContainers.add(trunkTwo);
// System.out.println(lootContainers.toString());
System.out.println(lootContainers.get(2).toString());
}
}
package TaskTwo;
/**
*
* @author Steven
*/
public class Lootable {
public String contents;
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public Lootable() {
this.contents = "gold coin, ruby, crown.";
}
}
package TaskTwo;
/**
*
* @author Steven
*/
public class Trunk extends Lootable
{
public Trunk() {
}
public Trunk(String newContents) {
this.contents = "gold coin, ruby, crown";
}
public String toString(){
return this.contents.toString();
}
}
This code will not due everything I am wanting. I am showing inheritence though the Trunk class inheriting from Lootable. I need to show overriding can be used to make the trunk not lootable or lock the trunk. I need to show polymorphism in the app. I think I have but I still am not clear on that concept. Any help would be appreciated