1

I have the following abstract Java class:

abstract class AbstractDto {

  String id;

  public void setId(String id) { this.id = id; }    

  public String getId() {return id; }

}

I also have class which extends the abstract class:

class SomeDto extends AbstractDto {

  @SomeAnnotation
  String id;

}

I want to annotate an instance variable derived from an abstract class. I am not sure if this is the way to go as I did it. I know that Java does not provide variable overloading so this is shadowing.

So what happens if I do:

public void go(AbstractDto dto) {
  println("dto.id: "+dto.id);
}

AbstractDto dto = new SomeDto();
dto.setId("1234");
go(dto);

Since I do shadowing when I set the id of SomeDto then there is an idvariable inherited from AbstractDto which is still not set.

How can I annotate an instance variable defined in an abstract super class?

Edit: When I do:

SomeDto dto = new SomeDto();
dto.setId("123");

Which id was set the one in AbstractDto or the one in SomeDto? What happens I a method in the Abstract class reads from id which id is then used?

Michael
  • 32,527
  • 49
  • 210
  • 370
  • It's called [`shadowing`](http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.4.1). This may be helpful: http://stackoverflow.com/questions/16941179/variable-shadowing-in-java – PM 77-1 Feb 06 '15 at 22:44
  • The only derivation here is of the class itself. The `id` variable in the derived class is not derived, inherited, etc. – user207421 Feb 06 '15 at 23:48

1 Answers1

1

The example you provided is called shadowing, there is no overriding for fields in Java.

More about shadowing could be found in Wikipedia:

In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope.

So, to use your annotation, you can either annotate your id variable in the abstract class (all inherited classes could use that annotated variable), or just annotate the id variable of SomeDto but you have to be aware that it's a new variable (no relation with the id's super class variable)

Regarding your second question:

Which id was set the one in AbstractDto or the one in SomeDto?

The id of SomeDto will be set because the reference variable type of dto is SomeDto. To explain more, in the inherited class SomeDto you defined another id variable which hides the first one, so each call using a reference to that class or any inherited class of SomeDto will call the new id variable defined in the SomeDto class.

What happens I a method in the Abstract class reads from id which id is then used?

Each call to id in the abstract class will use the id in the abstract class, so a method implemented in AbstractDto and using the variable id will use the AbstractDto one.

Annotating instance variables defined in abstract super class:

This is some real life examples where instance variables of abstract classes are annotated:

Community
  • 1
  • 1
Tarik
  • 4,961
  • 3
  • 36
  • 67
  • Please see my edit: Which id was set the one in AbstractDto or the one in SomeDto? – Michael Feb 06 '15 at 23:02
  • What happens I a method in the Abstract class reads from id which id is then used? – Michael Feb 06 '15 at 23:08
  • @confile is that answer your question or you have other questions? please feel free to ask – Tarik Feb 07 '15 at 00:38
  • How can I annotate an instance variable defined in an abstract super class? Such that the Abstract class can still use the variable. – Michael Feb 07 '15 at 00:40
  • @confile just annotate it like any instance variable, no matter if tha class is abstract or not, you can find examples [here](http://stackoverflow.com/questions/2921899/spring-abstract-class-and-annotations) or [here](http://stackoverflow.com/questions/7184556/spring-and-abstract-class-injecting-properties-in-abstract-classes) – Tarik Feb 07 '15 at 00:47