开发者

Java: can I require a child class to define a property value?

开发者 https://www.devze.com 2023-01-11 04:46 出处:网络
I have an abstract base class that many classes extend.I\'d like all these classes to define a un开发者_如何学编程ique value for a specific property that is originally defined in the base class (simil

I have an abstract base class that many classes extend. I'd like all these classes to define a un开发者_如何学编程ique value for a specific property that is originally defined in the base class (similar to the serialVersionUID property that causes a warning when not defined in classes that inherit from Serializable).

Is there a way for me, within my abstract base class, to declare a property that does not have a value, but requires all extended classes to define a value for it?

Note that the value does not have to be associated with each individual instance, i.e.: it can be defined as static.

Edit: I guess a more basic question I should also ask, since the answers vary so widely, is how does Java implement serialVersionUID (in terms of its signature) such that my IDE raises warnings when it's not defined?


Fields can not be overridden, but methods can. If you need not enforce that the value is constant, use:

class abstract Super() {
    /**
     * @return the magic value
     */
    abstract MyType getValue();
}

If you do need to enforce that the field is final, you can give each instance its own copy/reference as in the answers of Steve or Mike.

If you can't do that either you can do crazy stuff like:

class Super() {
    private static Map<Class<? extends Super, MyType> map = new ...;

    protected Super(MyType value) {
        if (!map.contains(getClass())) {
            map.put(getClass(), value);
        } else {
            assert map.get(getClass(), value) == value;
        }
    }
}


Hardly. I'd suggest putting user instructions in your base class' javadoc and checking if property is present before using it: object.class.getDeclaredField("MY_CONSTANT")

I think, Serializable interface is a good example of such approach.


Something like the following is a typical approach to this:

public abstract class Superclass {

    private final String something;

    public Superclass(String something) {
        this.something = something;
    }

    public String getSomething() {
        return something;
    }
}

public class Subclass {

    public Subclass() {
        super("a value for something");
    }
}

You don't get to have a static property value though.


Reposting to match the criteria I missed:

public abstract class Abstract {

    private static int field;

    public Abstract(int aValue) {
        field = aValue;
    }

    public int getField() {
        return field;
    }
}

public class Child extends Abstract {

    private static final int value;

    static {
        value = 10; //Pick a value for each class.
    }

    public Child() {
        super(value);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消