1

I want to define an abstract properties class with standardized name for attributes. That way all properties inheriting will declare the attribute with the same name pattern.

And on a sub class, I wanted to be able to add javax.validation.notNull annotation on some of those attributes.

Is it possible ? I see there is an open ticket for overriding super type annotation, which means it's not possible to override them, but here I want to add annotation.

TheBakker
  • 2,852
  • 2
  • 28
  • 49

1 Answers1

1

Yes, this is possible. You just need to override the method in the super class and add the desired annotation in the subclass.

In answer to your comment

Edit 1:

Yes, I meant overriding the getter. I'd strongly recommend against defining the same variable in a parent and child class. I'd consider accessing variables defined in a parent class as a violation of encapsulation. Apart from that, you can get into all sorts of troubles as those variables are NOT actually overridden. Consider the following Base class

public class Base {
    public int ggg;

    public void test() {
        System.out.println("Base test was called");
    }
}

and the Child class

public class Child extends Base {
    public int ggg;

    public void test() {
        System.out.println("Child test was called");
    }

}

Running main() int the following Test class

public class Test {
    public static void main(String[] args) {
        Base base = new Child();
        base.ggg = 5;
        System.out.println(base.ggg);
        System.out.println(((Child) base).ggg);
        base.test();
        ((Child) base).test();
    }
}

you will get the following output

5
0
Child test was called
Child test was called

The variable ggg exists twice, once in the child class and once in the super class. Depending on how you access the object, you access either one of them. This can not happen with a properly overridden method, as shown when calling test().

Besides, AFAIK, validation should work the same if the annotations as defined on the variable or on the getter.

Just as a side note. "overriding" static methods yields the same (mis-)behavior as "overriding" a variable and is therefore also discouraged.

Guenther
  • 2,035
  • 2
  • 15
  • 20
  • You mean the getters ? I wanted to put annotation on the attribut themself, so they throw exception on setters – TheBakker Sep 02 '19 at 21:12
  • Thanks, did not imagine one second to duplicate the attributes, problem with setting validation on getter, is it wont get triggered till someone call the getter, unlike on attribute. – TheBakker Sep 04 '19 at 14:27
  • If you didn't want to duplicate the attributes, how were you going to add different annotations to them in subclasses? What makes you think that validation on attributes is immediate vs delayed for those declared on getters? When a bean is validated, all validation annotations are prosessed. The only difference is that in the former case the validation is done on the value read from the variable and in the latter case against the value returned from the getter. – Guenther Sep 04 '19 at 16:52
  • was hoping for an javax.validation annotation that would override the parent one. – TheBakker Sep 05 '19 at 10:49