Pass ArrayList<> Object to Class that extends ArrayList

Darnoc Eloc :

I am trying to create a Class that extends ArrayList adding two methods as defined below. However, the new BetterArrayList class does not properly read in parameters from its constructor method to create the extended object from the ArrayList<> object that was passed. What is the proper approach for passing an ArrayList<> object to a class such as this and assigning the contents of that object to the class member variables?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class BetterArrayList<E> extends ArrayList<E> {
    private int size;
    private ArrayList<E> myArray = new ArrayList<>(size);

    public BetterArrayList(ArrayList<E> passArr){
        System.out.println("New BetterArrayList");
        this.size = passArr.toArray().length;
        this.myArray = passArr;
        myArray.addAll(passArr);    
    }
    public E pop() {
        return remove(this.size() - 1);
    }
    public void print() {
        myArray.forEach(elem ->{
            System.out.println(elem);
        });
    }
    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    public static void main(String[] args) {
        // int[] ints = new int[] {1,2,3,4,5};
        Integer[] ints = new Integer[] {1,2,3,4,5};
        System.out.println(ints.length);
        ArrayList<Integer> arrs = new ArrayList<Integer>(Arrays.asList(ints));
        System.out.println(arrs.size());
        BetterArrayList<Integer> BetterArray = new BetterArrayList<Integer>(arrs);
        System.out.println(BetterArray.size());
        BetterArray.pop();
        BetterArray.print();
    }

}

Nexevis :

You are getting confused by attempting to use both composition AND inheritance.

Put simply, composition is where you are attempting to put the ArrayList in as a class field and use it by accessing that field.

Inheritance is where you are attempting to use extends (in this case).

I modified your code to eliminate the composition and simply use extends (I changed the test print statements a bit to make it clear):

public class BetterArrayList<E> extends ArrayList<E> {

    private static final long serialVersionUID = 1L;

    public BetterArrayList(ArrayList<E> passArr) {
        this.addAll(passArr);
    }

    public E pop() {
        return this.remove(this.size() - 1);
    }
    public void print() {
        this.forEach(elem ->{
            System.out.println(elem);
        });
    }

    public static void main(String[] args) {
        // int[] ints = new int[] {1,2,3,4,5};
        Integer[] ints = new Integer[] {1,2,3,4,5};

        ArrayList<Integer> arrs = new ArrayList<>(Arrays.asList(ints));
        BetterArrayList<Integer> betterArr = new BetterArrayList<>(arrs);

        System.out.println("ArrayList: " + arrs);
        System.out.println("BetterArrayList: " + betterArr);

        betterArr.pop();
        System.out.println("BetterArrayList After Pop: " + betterArr);
    }

}

Output:

ArrayList: [1, 2, 3, 4, 5]
BetterArrayList: [1, 2, 3, 4, 5]
BetterArrayList After Pop: [1, 2, 3, 4]

Side Note:

I modified the name you used for your BetterArrayList instance to use proper Java naming conventions, withCasingLikeThis for a variable name, rather than CasingLikeThis.

Additionally, composition could be a perfectly valid way to do this as well, but try not to use both at the same time on the same class.

Tip to differentiate them:

Composition represents a has a relationship.

Inheritance represents a is a relationship.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=3709&siteId=1