Search code examples
javaarraylistextendsprotected

Why ArrayList.removeRange() is not visible in the RoleList Class that extends ArrayList?


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);
    }
}

Solution

  • protected means that it can be called from a subclass in the context of itself; protected does not mean that class B1 extends A can call protected methods of A on class B2 extends A.

    Example:

    class A {
        protected void protectedMethod() {
            //do something
        }
    }
    
    class B1 extends A { //<-- we are extending A
        public void someMethod() {
            B1 b1 = new B1();
            b1.protectedMethod(); //<-- you can call it because B1 extends A and you're in the context of B1
        }
    }
    
    class B2 { //<-- we are not extending A
        public void someMethod() { //<-- this method is exactly the same than above
            B1 b1 = new B1(); //<-- B1 is still extending A
            b1.protectedMethod(); //<-- though, you can't call the protected method anymore because you're not in the context of itself
        }
    }