I'm fairly new to Java, I have a linkedList of class ObjectType, I have various subclasses of ObjectType being added to the linkedList, when I pull the object out I am forced to pass it a type of ObjectType and seem to lose the inherited properties of the subclass.... Can anyone shed some light on this for me?
//LIST STORING OBJECT
public class MyObjectList{
private LinkedList<ObjectType> queue = new LinkedList<ObjectType>();
public MyObjectList()
{
queue = new LinkedList<ObjectType>();
}
public ObjectType addObject(ObjectType myObject)
{
queue.add(myObject);
return myObject;
}
}
//MY BASIC OBJECT
public abstract class ObjectType {
public int objectWidth;
public ObjectType () {}
}
//MY EXTENDED OBJECT BOX
public abstract class Box extends ObjectType {
public int objectWidth = 50;
public ObjectType () {}
}
//MY EXTENDED OBJECT PLATE
public abstract class Box extends ObjectType {
public int objectWidth = 25;
public Box() {}
}
// OK so now if I add a load of boxes to my list of boxes will have their objectWidth set to zero!
mList = new MyObjectList;
for (int i=0;i<10;i++)
{
mObject = mList.addObject( new Box );
}
It's important to remember that my list is supposed to be made up of different object types eventually so the baseObject needs to remain as it is. if possible!
new Box()is it is abstract. Can you provide an example which both compiles and runs?