1

I'm currently trying to learn Java and I encountered a problem on which I'm a little stuck.

I have one superclass and two subclasses that extend it, in the superclass I created an Arraylist that I want to use in the constructor of the subclasses but I'm not quite sure about my solution.

I would be glad for any help and please let me know if you need any further information in order to understand my question.

So here is what I've tried:

public class Superclass{ 
    private String something;
    private ArrayList<String> list;

    public Superclass(String something){ 
      this.something = something; 
      list = new ArrayList<String>(); 
    }
}


public class Subclass extends Superclass{ 

     public String someelse; 

     public Subclass(String name, ArrayList<String> list, String 
                   someelse){ 
          super(name); 
          this.someelse = someelse; 
          list = new ArrayList<String> 
     }
}

It doesn't through out any errors but is it the right way to do it like that, or is there a way to have the ArrayList in the super brackets? I want to initilize it the superclass, add elements in the subclasses and use it in a different class.

pentaquark
  • 21
  • 7
  • if you want to access the list in the subclass, it must be declared non `private` (so either `protected` or `public` or package default) and then you need to initialize it only in the super – Sharon Ben Asher Jul 14 '19 at 12:59
  • 1
    This code is not valid Java code. So it can't possibly compile. So no, it's not the correct way to do what you want to do (whatever that might be). – JB Nizet Jul 14 '19 at 13:00
  • Why do you want to assign an empty ArrayList to `list` in the subclass ctor, if you have already done this in the superclass ctor? – Andy Turner Jul 14 '19 at 13:09
  • So you're saying that I can set it to protected in the superclass and then only initialize it there, rather than initializing it twice in both? – pentaquark Jul 14 '19 at 13:25

1 Answers1

0

My apologies, I did not fully understand the question. I would recommend having the list in Superclass as protected, which allows your Subclass to access it discretely, without public access. See below:

public class Superclass {

    private String name;
    protected List<String> list;

    public Superclass(String name) {
        this.name = name;
        this.list = new ArrayList<>();
    }
}

public class Subclass  {

    public String someName;

    public Subclass(String name) {
        super(name);
        this.someName = name;
    }

    public void add(String string) {
        list.add(string); <- because of the list being protected, this refers to the list in the super class
    }
}

My advice though would be to leave the adding and removing of elements to the Superclass, unless you are planning on having any extra checks in the Subclass when adding/removing elements from the list. This would mean moving the add method to the Superclass as such:

public class Superclass {

    private String name;
    protected List<String> list;

    public Superclass(String name) {
        this.name = name;
        this.list = new ArrayList<>();
    }

    public void add(String string) {
        list.add(string);
    }
}

public class Subclass  {

    public String someName;

    public Subclass(String name) {
        super(name);
        this.someName = name;
    }
}

You would then create and use it as such (works for both examples above):

Subclass subclass = new Subclass("sub1");
subclass.add("string1"); <- gets added to superclass list

For more info on scope identifiers, see this.

Kris Rice
  • 559
  • 1
  • 5
  • 23
  • Why like so? What does this do? What did you change? Why did you change it? A good answer contains explanation, it doesn't simply dump code. – Andy Turner Jul 14 '19 at 13:11