2

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

}

4
  • I'm confused as to why you take an arraylist and add it to itself in your constructor. What are you trying to accomplish here? Commented Feb 20, 2020 at 20:57
  • 1
    You don‘t need myArray in your class. Just call super(passArr) in your constructor and work with your additional methods on „this“. Commented Feb 20, 2020 at 21:04
  • 3
    To spell it out I think the idea is you are attempting to use both composition AND inheritance with the same class. Commented Feb 20, 2020 at 21:05
  • Thanks! All clear. Commented Feb 20, 2020 at 21:32

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Simple inheritance was the objective but I did not invoke the parent class's constructor method.
I have heard that composition is "better" than inheritance because it allows for more flexibility without the hierarchical rigidity. I know this may be a broad generalization, but, how accurate is this claim with respect to common applications?
@Darnoc Eloc I don't know if I would say "better", but perhaps more useful. From my experience it has been more common to need to include multiple other custom classes inside of another custom class rather than extend functionality of a class to another. For example, a Banking application might have a Customer class which contains a SavingsAccount class, but a SavingsAccount is NOT a Customer, so you wouldn't extend, you would use composition.
@Darnoc Eloc A useful example of inheritance could be making a video game where all enemies have default functionality, AKA Enemy Class, and a specific enemy would extend that class and be something such as Goblin extends Enemy, because Goblin IS an Enemy. You can go multiple layers deep too - Enemy class, Monster extends Enemy, Goblin extends Monster etc.
0

Added super in constructor method to automate creation of new BetterArrayList<> object with all the attributes of the ArrayList<> object that was passed as an argument. Seems to be a simple fix, I was trying to manually inherit the attributes of the parent class instead of just invoking super.

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

    public BetterArrayList(ArrayList<E> passArr){
        super(passArr);
        System.out.println("New BetterArrayList");
    }
    public E pop() {
        return remove(this.size() - 1);
    }
    public void print() {
        System.out.println(this);
    }
    /**
     * 
     */
    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.print();
        BetterArray.pop();
        BetterArray.print();
    }

}

Comments

0

Here is My Solution. Tried to Simplify and Understandable.

ArrayMain.java

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

public class ArrayMain {

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

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

        System.out.println("Array List: " + arrs);

        betterArrayList.pop();
        betterArrayList.print();

        betterArrayList.pop();
        betterArrayList.print();
    }
}

Now Add BetterArrayList extending Arraylist and Add Pop and Print Statement:

import java.util.ArrayList;

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


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

    public void pop() {

        System.out.println("Element Poped is: " + this.remove(this.size()-1));
    }


    public void print() {
        System.out.println("New Array List: " +  this);
    }
}
 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.