sowdust
sowdust

Reputation: 87

java inheritance: in the parent method write code that uses child variable

I am trying to write a general GUI class that contains main methods used by all different and more specific GUIs.

public class GeneralGUI extends javax.swing.JFrame {
    protected javax.swing.JTextArea logTextArea;
    protected void say(String s) {
        logTextArea.append(s + "\n");
    }
}

the subclass class contains:

public class SpecificGUI extends GeneralGUI {
    protected javax.swing.JTextArea logTextArea;
    [...]
    private void initComponents() {
        [...]
        logTextArea = new javax.swing.JTextArea();
    }
}

what I want is that when I call

SpecificGUI myGUI =  new SpecificGUI();
myGUI.say("Ciao!");

the "Ciao!" string is appended to myGUI.logTextArea, but this doesn't happen: it insteads throws a Null Pointer Exc because it is looking for the logTextArea belonging to GeneralGUI - an object that hasn't been instantiated.

1) why is it so? 2) how can I achieve the desider result?

thanks!

EDIT: I have tried removing the variable declaration in the subclass: being it a GUI I designed through netbeans, netbeans won't let me remove the declaration!

Upvotes: 0

Views: 478

Answers (5)

Shubhradeep
Shubhradeep

Reputation: 1

Please remove the protected javax.swing.JTextArea logTextArea in the child class.

Next in you parent protected javax.swing.JTextArea logTextArea is not defined and is null, it is a object and perhaps needs to be initiated prior to append. Please check.

Upvotes: 0

DeepInJava
DeepInJava

Reputation: 1961

protected javax.swing.JTextArea logTextArea   

hides the member variable of parent class GeneralGUI and you haven't initialize logTextArea with nothing so it remains Null that's why you got NullPointerEception. You should just remove member variable logTextArea from SpecificGUI class

Upvotes: 1

Amit Jain
Amit Jain

Reputation: 143

1-logTextArea is not getting instantiated thats why you are getting NPE. 2-You have to decide where you want logTextArea as class attribute.i think it does not make any sense keeping it in both classes,else try and instantiate logTextArea of generalGUI class.

Upvotes: 1

Alex Shesterov
Alex Shesterov

Reputation: 27555

1) You've already answered the question — the say() method of the parent class it is looking for the logTextArea inside the parent class, where it is declared. Your re-declaration of the field in the child class creates another field, which hides the same-named field from the parent class.

2) Just remove the declaration of another logTextArea in child class.

If you really need another field in the child class with the same name, then you can access the (hidden) parent field via super.logTextArea.

Upvotes: 1

user2717954
user2717954

Reputation: 1902

you dont need to define

protected javax.swing.JTextArea logTextArea

twice.

defining it as a protected field of parent class means all inhereting classes has access to this field.

inheritance works like this:

1) You call parent method foo() from child class B.

2) if child class has method foo() it is invoked

3) if child has no method foo look up the chain of inheritance for a method foo() and invoke it

4) say method foo() in parent class A uses field x, it can only access the field x if A or any super class has it.

Upvotes: 1

Related Questions