1

I am currently working on a project and I have an array of Objects. I know that the array will store the following types: Product, Food, Clothes (food and clothes are both subclasses of Product). The thing is that I am trying to access an attribute of an object from the array (barCode), but when I try to do that it gives me an error and says that "barCode is undefined for type Object".

How can I solve this problem?

I cannot create an separates array for every type because it won't be scalable when I would add more classes and types.

Thanks!

yes. the future classes will also be sublcasses of Product

this is one instance of the code. the only problem is on the lines with getBarCode() (which is a method for the Product class)

private Object arr[] = super.getArray();

public void sort(int c)
{
    Object aux;
    int min = 999, poz;

    for(int i = 0; i < super.getIndex() - 1; i ++)
    {
        for(int j = i; j < super.getIndex(); j ++)
        {   
            if( arr[j].getBarCode() < min)
            {
                min = arr[j].getBarCode();
                poz = j;
            }
        }
        aux = arr[i];
        arr[i] = arr[poz];
        arr[poz] = aux;
    }
}
3
  • Can we see the code please Commented Nov 29, 2014 at 0:45
  • Will all future classes be sub-classes of Product as well? Commented Nov 29, 2014 at 0:46
  • You have to cast the Object to either Product, Food or Clothes after checking which one it is via instanceof, or you make an abstract superclass and define your barCode in there Commented Nov 29, 2014 at 0:50

2 Answers 2

1

If you could guarantee that the method barCode exists on Product and its children, then you could just use an array of Product instead.

You would have to deal with any sort of fiddly casting you would want to do if you want to use specific child class methods, but that would at least let you call the same method on all elements without too much worrying.

So long as getArray can be changed to return Product, then you can write this:

private Product[] arr = super.getArray();

You'd change aux to a type of Product, too.

Sign up to request clarification or add additional context in comments.

Comments

0

When accessing the object from the array, what you can do is compare the object type.

Like this:

Object o = array[0];
if(o.getClass().equals(Product.class)) {
   Product p = (Product)o;
   p.getBarcode();
}

Alternatively, if all of your objects you are putting into this array are subclasses of Product, just make an arrayList to use Products, like new ArrayList(), and then call the barcode method for there.

4 Comments

it says that it cannot cast for Object to Product
You cannot cast a Object[] array to a Product[] array, but you can cast a Object member to a Product. Product p = (Product)o;
Your array access approach is incorrect - should be array[0] or wherever the access needs to happen. get is available on Lists.
@Makoto fixed, couldn't remember if the question had arrays or arrayLists

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.