Referencing instance variables from within abstract class

Shane Atchley

I have an abstract class which lays out what each of its subclasses should implement. Most methods will have different implementations for each subclass, but some will be the same for all. I want the methods that will be the same to be to be defined in the abstract class, so that I'm not pasting the same method into several different classes. However, when I call that method on an instance of the abstract's subclass, I receive a nullpointer because, I imagine, the method implemented in the abstract class is referencing the abstract's field, not the instance's field.

Can someone point out where the flaw is?

For example:

abstract class ControlView {
    String[] controls;

    abstract void render();

    void release() {
        for (int i = 0; i <= controls.length; i++) {
            //Release the controls
        }
    }
}



class StartingControls extends ControlView{

    String[] controls;
    Button uiDrawButton;
    Button uiLoadButton;


    StartingControls() {
        this.controls = new String[2];

        uiDrawButton = new Button();
        this.controls[0] = uiDrawButton;

        uiLoadButton = new Button();
        this.controls[1] = uiLoadButton;
    }


    public void render() {
        //Unique Render implementation
    
    } 
}

When I call

instanceOfStartingControls.release();

I obviously want to iterate over the two strings that I put into instanceOfStartingControls' controls field when it was constructed. I do not want to iterate over the non-initialized array that is apparently living in the abstract.

Is it some combination of access modifiers or static methods that is keeping this from working as it seems it should, or am I missing some crucial bit of knowledge on abstract classes? This feels like a basic question, but I'm having a hard time putting it to words, so I've not been satisfied with any results from my searches.

user14387228

There are two arrays called controls. The one in the derived class is obscuring the one in the base class, thus the base instance never gets set non-null.

Delete the declaration from the derived class.

This issue is not related to the base being abstract. If you use the same field name in a derived class as is used in a base class, the base instance will be obscured.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Referencing non static variable from within static Inner Class

Generics: Create instance from abstract class

Referencing class instance through lists

Scala abstract class instance

Referencing variable in class from within the same class

Typescript: instance of an abstract class

Java abstract class - Should the instance variables be private or protected?

Derived class using variables from abstract base class C++

How to create an instance of concrete class from static method of abstract class

How to refer to the instance of a class from within the instance

Referencing an instance of a class created in main from another class module

Creating dynamic variables for a class from within the class

How to pass an instance of of a class from within that class

Referencing a variable declared in abstract class from inherited class

Remove instance of a class from a vector within a vector

How to instantiate a new object from an abstract class containing an instance of a HashMap?

How to annotate an instance variable derived from an abstract Class?

Can an instance from one class change variables in an instance of another class?

Get Class Instance from Within Decorator

Is it possible to modify variables in different class from within App() without instantiating new instance of class

Changing vue instance variables from within component

Ruby instance variables within class?

Referencing method from abstract class

Python type annotations for instance of class derived from abstract base class

Share variables within the instance of a class without adding them as attributes

C# Access base class variables/functions from abstract class

Return instance of deriving class from abstract generic method

How to declare instance variables in abstract class?

Unable to clone an object from within an abstract class method in Scala