0

I've got a generic class, which declares some fields and a constructor which works with them:

public abstract class GenericClass extends JFrame
{
    protected static String FIELD_1;
    protected static String FIELD_2;
    protected static String FIELD_3;

    public GenericClass()
    {
        super(FIELD_1+" "+FIELD_2+FIELD_3);
    }
}

And child classes which should hide fields and use the superclass constructor:

public class ChildClass1
{
    protected static String FIELD_1 = "hello";
    protected static String FIELD_2 = "world";
    protected static String FIELD_3 = "!";

    public ChildClass
    {
        super();
    }
}
public class ChildClass2
{
    protected static String FIELD_1 = "another";
    protected static String FIELD_2 = "class";
    protected static String FIELD_3 = "!";

    public ChildClass
    {
        super();
    }
}

I don't get it why created JFrames have title null nullnull. What am I doing wrong?

update

Using of these classes is quite simple:

public class Main
{

    public static void main(final String[] args)
    {
        new ChildClass1();
        new ChildClass2();
    }
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Danny Lo
  • 1,553
  • 4
  • 26
  • 48
  • You've listed three classes, but no code using them. How are you using these classes? – Rajit Mar 29 '14 at 20:52
  • 1
    Also: [Overriding a super class' instance variables](http://stackoverflow.com/questions/427756/overriding-a-super-class-instance-variables) – Rajit Mar 29 '14 at 20:54
  • @Rajit Athough it is irrelevant for my question, see update – Danny Lo Mar 29 '14 at 20:54
  • It's not possible override a superclass's instance OR class variables. You'll need to set those fields via setters or a constructor. But then you might reconsider using static. – Rajit Mar 29 '14 at 20:56
  • Your fields in `GenericClass` are completely independent of the similarly named fields in the subclasses. For example, `ChildClass.FIELD_1` is "hello", and `GenericClass.FIELD_1` is null. Even from within `ChildClass`, `GenericClass.FIELD_1` is still null. It is unrelated to `ChildClass.FIELD_1` with the exception that `FIELD_1` used without a class name from within `ChildClass` ends up referring to `ChildClass.FIELD_1`; simply because that's a convenience that the language provides for you. You mentioned "hiding fields" in your title, so it seems you already know how this works? – Jason C Mar 29 '14 at 21:04
  • @JasonC I know how this works but I was just a little confused at this late hour... – Danny Lo Mar 29 '14 at 21:26

1 Answers1

1

because GenericClass has null values for those FIELD_1, FIELD_2, FIELD_3

public abstract class GenericClass extends JFrame
{
    protected static String FIELD_1;
    protected static String FIELD_2;
    protected static String FIELD_3;

    public GenericClass()
    {   // null +" " +null+null = `null nullnull`
        super(FIELD_1+" "+FIELD_2+FIELD_3);
    }
}

This was the idea: GenericClass should just DECLARE fields and subclasses should initialize them. – Danylo Esterman 41 secs ago

You can't have it that way for fields, You need to overload abstract class's constructor and force subclass to pass parameters during constructor (by hiding default constructor) and then use those passed parameter for JFrame's constructor

jmj
  • 237,923
  • 42
  • 401
  • 438
  • This was the idea: `GenericClass` should just DECLARE fields and subclasses should initialize them with class-specific values. – Danny Lo Mar 29 '14 at 20:51