Inheritance issues

Having some issues with inheritance. I have a parent called IntArrayBag which has an ensureCapacity method which more than doubles the size of my array when I run out of space to add more ints...

I am attempting to calling it from the child, a set bag called FinalBag in an attempt to expand my local private int array of the same name (as the parents private int array). Is this possible? I am not erroring but am very aware that it is NOT getting its size increased.

From parent:

publicvoid ensureCapacity(int minimumCapacity)

{

int[ ] biggerArray;

if (data.length < minimumCapacity)

{

biggerArray =newint[minimumCapacity];

System.arraycopy(data, 0, biggerArray, 0, manyItems);

data = biggerArray;

}

}

Child class:

publicclass FinalBagextends IntArrayBag{

privateint[ ] data;

privateint manyItems;

public FinalBag(){

finalint INITIAL_CAPACITY = 1;

manyItems = 0;

data =newint[INITIAL_CAPACITY];

}

publicvoid add(int element)

{

if (manyItems == data.length)

ensureCapacity((manyItems + 1)*2);

if (!inArray(element)){

data[manyItems] = element;

manyItems++;

}

}

private Boolean inArray(int n){

for (int i = 0; i < manyItems; i++)

if (data[i] == n)

returntrue;

returnfalse;

}

}

[2888 byte] By [Devina] at [2007-11-26 22:22:18]
# 1

If you add an attribute with the same name to both classes, they will each simply have their own copy. You cannot override attributes like you can override methods.

If you really want to use this class design, you should provide a protected getter method for the manyItems array in your parent class so your child class can query for it. That way both classes can use the same array. But it seems more logical to me that all methods should be part of the IntArrayBag class and that your FinalBag is simply redundant.

gimbal2a at 2007-7-10 11:20:42 > top of Java-index,Java Essentials,New To Java...