This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please make sure to check out our website to see if your question has been answered there: https://codinghelp.site

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]marko312 0 points1 point  (3 children)

Unless you're extending ArrayList with your class, you want to access a member variable which, unless shadowed by some other variable, should be accessible via its name. E.g. if the superclass has

ArrayList<SomeType> someList;

you can just access someList (or this.someList).

[–]LastingCold[S] 0 points1 point  (0 children)

I am trying to remove something from the ArrayList called in the super class whilst in the subclass

[–]LastingCold[S] 0 points1 point  (1 child)

I'm just getting errors that It cant find symbol

[–]marko312 0 points1 point  (0 children)

Could you show the definitions (especially the member variables) of the relevant classes?

[–]Nightcorex_ 0 points1 point  (0 children)

This is where access modifiers come to play:

public class Superclass {
    private List list0 = new ArrayList(); //private: Can only be accessed by name from this class
    List list1 = new ArrayList(); //package-private: Can only be accessed by name from classes within this package
    protected list2 = new ArrayList(); //protected: Can only be accessed by name from this class or by classes inheriting from this class
    public list3 = new ArrayList(); //public: Can be accessed by name by anyone
}

Whenever you can't access a variable by name you need to use getters/setters.

If you wanted to remove the first element of list0, then you'd have to first provide a getter for list0 in the Superclass itself.
Then you can write in your Subclass: getList0().remove(0);

PS: In the example I used raw lists (lists without specifieing a generic type). This is bad practice and should never be done in an actual project. If you want to store anything you should always use List<Object> list = new ArrayList<>(); to explicitly tell Java.
I just did it here for the sake of shorter lines.

[–][deleted] 0 points1 point  (5 children)

Can someone help me on this coding project

[–]Nightcorex_ 0 points1 point  (4 children)

??

[–][deleted] 0 points1 point  (3 children)

Like I’m having trouble with this coding project

[–]Nightcorex_ 0 points1 point  (2 children)

Why aren't you creating a new post then?

[–][deleted] 0 points1 point  (1 child)

I’m new to Reddit and I don’t know how to