The method: protected void removeRange(int fromIndex,int toIndex)
in class ArrayList
is protected
, so I cannot call it on an ArrayList object, but I can call it on an object from a class that extends ArrayList, as the code below shows. However, we have classes in Java API that extend ArrayList like: javax.management.relation Class RoleList
Why I cannot call removeRange()
on an object of type RoleList
like I did with my class: ExtendsArrayList?
public class ExtendsArrayList extends ArrayList<Integer> {
public static void main (String[] args) {
ExtendsArrayList list = new ExtendsArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
// I can call removeRange() here.
list.removeRange(1, 3);
RoleList rl = new RoleList();
rl.add(1);
rl.add(2);
rl.add(3);
rl.add(4);
rl.add(5);
// The method removeRange(int, int) from the type ArrayList<Object> is not visible
// Why its not visible in a class that extends ArrayList?
//rl.removeRange(1, 3);
}
}
EDIT:
Why I should extends RoleList
for this to work? RoleList extends ArrayList
aromatically, why I cannot expect to call removeRange()
on an object of RoleList
to just work? On the other word, why ArrayList.removeRange()
is not visible to an object of type RoleList
when RoleList extends ArrayList
, when it IS visible to an object of my class: ExtendsArrayList extends ArrayList
? ExtendsArrayList
is extended by me and RoleList
is extended in Java API.
public class ExtendsRoleList extends RoleList {
public static void main (String[] args) {
RoleList rl = new RoleList();
rl.add(1);
rl.add(2);
rl.add(3);
rl.add(4);
rl.add(5);
// The method removeRange(int, int) from the type ArrayList<Object> is not visible
// Why its not visible in when RoleList that extends ArrayList?
//rl.removeRange(1, 3);
ExtendsRoleList erl = new ExtendsRoleList();
erl.add(1);
erl.add(2);
erl.add(3);
erl.add(4);
erl.add(5);
// Why I should extend RoleList for this to work? Why its not visible when RoleList extends ArrayList automatically?
erl.removeRange(1, 3);
}
}