1

In our project we haven't Spring, so work like can with that. I found that we have many repetable code. We have something like that in 5 or more classes

import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;

@Data
public class QueryParamDto {

    @DefaultValue("1");
    @QueryParam("sortBy")
    private String sortBy;

    @DefaultValue("ASC");
    @QueryParam("sortDir")
    private String sortDir;
    
    ...
}

And it would be good to create something like BaseDto class with common fields, but in some classes we have different DefaultValue like

@Data
public class KeywordsDto {

    @DefaultValue("5");
    @QueryParam("sortBy")
    private String sortBy;

...

}

Because of that 'sortBy' can not be coommon fields with common value = 1 from BaseDto.

Maybe there is some variant to oveeride child's fields?

Dred
  • 1,076
  • 8
  • 24

1 Answers1

0

you can just modify it in your child class without the need of overriding, declare it in the father class and give it the value in the child. or check out this answer, maybe could help Overriding member variables in Java ( Variable Hiding)

F4hdC
  • 1
  • 1
  • What do u mean? Could you please clarify it with some of example? – Dred Oct 31 '22 at 15:19
  • I mean, you can declare it in you father class like public int variable, and in your child class u can just invoke the variable and giving it a value as you need, I'm not sure if I'm giving you the answer that you are really looking for ! – F4hdC Oct 31 '22 at 15:26
  • And I still don't understand it ((( How should I invoke in child class parent variable? like `super.sortBy=12` ?But where should I do that? In constructor? But How Can I be sure, that previous value like 0 created by `@DefaultValue` or by passed by client from Front-end – Dred Oct 31 '22 at 16:57
  • Yes exactly, you can do that in wherever you want always into the child class of course, I really recommend you to watch a video tutorial about heritance to understand as well how it works. the default value u can give it in the father's constructor. Always you instance an object you will get that default variable value. so also with super you can change it later. – F4hdC Oct 31 '22 at 17:18
  • Nope. I can't, because, as I told, When I'll override `wherever you want` I wouldn't know did that value come from parent as DefaultValue or it came from Front-End – Dred Nov 01 '22 at 06:12