2
class ABC {
    private int[] variable;
    public int[] getVariable() {
        return variable;
    }
    public ABC() {
        variable = new int[123456];
    }
}

class DEF extends ABC {
    public int[] getVariable() {
        return new int[0];
    }
} 

variable is used in ABC, but completely unused and needless in DEF. But I can't see any proper way to prevent creating this big array in DEF, because always some constructor of superclass has to be executed. I see only one, inelegant way: new, "fake" constructor for ABC:

protected ABC(boolean whatever) {}

Then in DEF I can write:

public DEF() {
    super(true);
}

and it works - variable isn't initialized.

But, my question is - can I solve this more properly?

Maybe if variable is unused, compiler automatically deletes her? It's quite often situation, when such feature could be useful.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Krzysztof Stanisławek
  • 1,267
  • 4
  • 13
  • 27
  • Interesting question. as `variable` is marked _private_ in ABC and the getter is overridden, there is absolutely no way for DEF to access it. I wonder also if the compiler is able to delete it in the optimization process. – jeroen_de_schutter Sep 13 '13 at 15:19
  • @jeroen_de_schutter It may also have to detect whether it's used via reflection, which could be a lot more difficult / impossible. – Bernhard Barker Sep 13 '13 at 15:23
  • Yeah ... reflection ... I always forget about that. It takes all the fun out of Java programming :-( – jeroen_de_schutter Sep 13 '13 at 15:25

1 Answers1

6

Are you sure DEF needs to extend ABC - I mean, is a DEF logically a ABC? Inheritance is powerful but needs to be used with caution.

In your case, I'd rather have:

public interface WithVariable {
  int[] getVariable();
}

And have both ABC and DEF implementing WithVariable. This way constructing a ABC object will initialise the needed variable, and constructing a DEF object won't do anything, but they'll both reply to the same message (getVariable()).

manub
  • 3,990
  • 2
  • 24
  • 33
  • +1 very good answer but if `DEF` is `ABC`, question remains unanswered – Nandkumar Tekale Sep 13 '13 at 15:22
  • 1
    I think the proper way to solve problem the OP asked is to use an `interface`, as the fact you're not using `variable` original value populated in the constructor is a smell that `DEF` is not really a `ABC`. – manub Sep 13 '13 at 15:25
  • Yes, I want DEF to be ABC. Sorry, I don't know how to insert bigger amount of code: http://wklej.org/id/1128907/ – Krzysztof Stanisławek Sep 13 '13 at 15:25
  • 1
    @KrzysztofStanisławek if that's the case, make the `Log` class abstract or create a `Log` interface. Also, with your current design, `BetterLog` has two `ArrayList entryList;`, one from super class and another that hides the field from super class. – Luiggi Mendoza Sep 13 '13 at 15:29
  • I agree with Luiggi - from your code it really seems to me that you should have an `interface`. `BetterLog` doesn't reuse current functionality of `Log`, you're also reassigning the `protected` variable. Extending `Log.Entry` is fine, but `BetterLog` and `Log` should implement the same interface IMHO. – manub Sep 13 '13 at 15:31