5

In Java, I have had several projects recently where I used a design pattern like this:

public abstract class A {
 public abstract int getProperty();
}

public class B extends A {
 private static final int PROPERTY = 5;

 @Override
 public int getProperty() {
  return PROPERTY;
 }
}

Then I can call the abstract method getProperty() on any member of class A. However, this seems unwieldy - it seems like there should be some way to simply override the property itself to avoid duplicating the getProperty() method in every single class which extends A.

Something like this:

public abstract class A {
 public static abstract int PROPERTY;
}

public class B extends A {
 @Override
 public static int PROPERTY = 5;
}

Is something like this possible? If so, how? Otherwise, why not?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • 1
    You do not override class variables in Java you hide them. Overriding is for instance methods. Hiding is different from overriding – Idos Jul 02 '16 at 18:30
  • Although you can use a `static` block: `static { PROPERTY = 5; }` – Idos Jul 02 '16 at 18:31
  • I don't think that `static` is essential to the question. To me, it looks like OP wants to know if there is a way to provide a different value without replacing the whole method. Voting to re-open. – Sergey Kalinichenko Jul 02 '16 at 18:36
  • correct - static is irrelevant to the question. i am looking for the way to implement properties on inherited classes with the least amount of code duplication possible – nhouser9 Jul 02 '16 at 18:40
  • @dasblinkenlight Why was this reopened? The linked question has nothing to do with `static`, it is an artefact of the example code, as shown by this answer http://stackoverflow.com/a/1686926/1743880 – Tunaki Jul 02 '16 at 18:48
  • See also http://stackoverflow.com/questions/2211002/why-not-abstract-fields – Tunaki Jul 02 '16 at 18:50
  • @Tunaki OP is not looking to override a class variable. He is using `static int PROPERTY` in his example, but my understanding is that all he wants to drop the override of the method from `B` in his top example, as opposed to making his second example work. In other words, he wants a shorter way to override something that can have an override, not a way to override something that cannot have overrides. Your second link is a lot closer to what I think OP wants to achieve. Had I seen that link before, I wouldn't have voted to reopen. – Sergey Kalinichenko Jul 02 '16 at 18:57

1 Answers1

12

You cannot "override" fields, because only methods can have overrides (and they are not allowed to be static or private for that).

You can achieve the effect that you want by making the method non-abstract, and providing a protected field for subclasses to set, like this:

public abstract class A {
    protected int propValue = 5;
    public int getProperty() {
        return propValue;
    }
}

public class B extends A {
    public B() {
        propValue = 13;
    }
}

This trick lets A's subclasses "push" a new value into the context of their superclass, getting the effect similar to "overriding" without an actual override.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523