I would like to know the reason for fragile base class problem in java.
Here is the code and i cant find the reason for triggering child class method even if i have given instruction to trigger a method in parent class
in public class Array
public class Array {
private ArrayList<Object> a = new ArrayList<Object>();
public void add(Object element)
{
a.add(element);
}
public void addAll(Object elements[])
{
for (int i = 0; i < elements.length; ++i)
add(elements[i]);
}
}
in ArrayCount class... this class is responsible for store the count of the list
public class ArrayCount extends Array {
public int count = 0;
@Override
public void add(Object element)
{
super.add(element);
++count;
}
@Override
public void addAll(Object elements[])
{
super.addAll(elements);
count += elements.length;
}
}
main class as follows
public class Main {
public static void main(String[] args) {
ArrayCount ac = new ArrayCount();
Object [] objArray = new Object[7];
objArray[0] = "xxx";
objArray[1] = "xxx";
objArray[2] = "xxx";
objArray[3] = "xxx";
objArray[4] = "xxx";
objArray[5] = "xxx";
objArray[6] = "xxx";
ac.addAll(objArray);
System.out.println("count is : " + ac.count);
}
}
Aucun commentaire:
Enregistrer un commentaire