0

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!

1
  • You can't add a new Box() is it is abstract. Can you provide an example which both compiles and runs? Commented May 11, 2013 at 16:07

3 Answers 3

2

You cannot override fields in Java, so if you want to set objectWidth to 50 in your subclass, you would do it like this:

public abstract class ObjectType {
    public int objectWidth;
    public ObjectType () {}
}

public abstract class Box extends ObjectType {
    public Box () {
        objectWidth = 50;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

As an off-topic matter, data encapsulation missing here. The field objectWidth should be private, and public getters/setters should be in place in the abstract class - overridden if needed in the Box class.
Thanks to everyone for their answers! I've learnt a lot. @Mena, if I have a lot of properties does the class not get a big code heavy doing getters and setters for every property? What are the advantages over simply setting it directly rather than using a method? Thanks!!
@JulianYoung I know it might look cluttered to have many properties AND their getters/setters around, especially when those getters/setters are performing the basest operations, without manipulating the arguments or results respectively. However, as your code develops, you might very well regret the choice to allow public fields as such (e.g. need to check for nulls, etc.). If you come from C#, consider that, although the code seems to encourage you to declare public properties, it actually does not. See also: en.wikipedia.org/wiki/…
1

Since variables aren't overridden, but hidden, the approach of declaring the variable again in each subclass and giving it a new value doesn't work.

One way to solve this is to give your abstract class a method, e.g. getObjectWidth(), and override that in each subclass. Since methods can be overridden, this way the specific value will be shown for each object.

Comments

0

A better pattern to use is a constant per class like.

//MY BASIC OBJECT
public abstract class ObjectType {

    public abstract int objectWidth();

    public ObjectType () {}

}

//MY EXTENDED OBJECT BOX
public class Box extends ObjectType {

    public int objectWidth() { return 50; }

    public ObjectType () {}
}

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.