Deriving from a class that has Annotation @PostConstruct
After testing this scenario, the @PostConstruct
method in the base class WILL automatically be called.
The flow goes like this:
- When the child class is created, you are in the constructor of the child class, you then are forced into the parent class automatically.
- Once the parent class constructor is done you are sent back to the child class' constructor.
- Once the child class constructor is done you are automatically sent to the PARENT classes
@PostConstruct
method
![Marquis Blount](https://i.stack.imgur.com/B5Hrn.jpg?s=256&g=1)
Marquis Blount
8+ years of professional Software Engineering experience. My preferred language is Java. I've used various different frameworks pertaining to Web, Desktop, and Mobile development. In the near future I want to get more involved with competitive programming.
Updated on June 02, 2022Comments
-
Marquis Blount about 2 years
If you have a parent class which uses the
@PostConstruct
annotation and you create a child class that derives from it. Will the@PostConstruct
method be called automatically each time an instance of the child class is created? since that@PostConstruct
method is called each time an instance of the the parent is created.I Know that in the child class it calls
super();
for us automatically without us having to call it.im just not sure if the
@PostConstruct
annotation is automatically called if that child class calls thesuper();
constructor. -
Basil over 9 yearsIn my test, if you're overriding a super's
@PostConstruct
, then the child's is called first. If you're not overriding, then the parent's is called first.