1

I have a peculiar requirement to call Parent class PostContruct instead of child class

import javax.annotation.PostConstruct;

public class Parent {

    public Parent(){
        System.out.println("In parent contructor");
    }

    @PostConstruct
    public void test(){
        System.out.println("In parent post construct");
        // call the child's class method 
        this.test()
    }
}


import org.springframework.stereotype.Service;

    @Service("Child")
    public class Child extends Parent{

        public void test(){
            System.out.println("In child post construct method");
        }
    }

So using Spring's service annotation child class gets loaded , Using PostContruct annotation the child's class test method is called , But our requirement is to call parent's class from where we can call the child's class test method .

Is there any way to achieve the same , We don't want to introduce new methods or change method logic

Akshat
  • 575
  • 2
  • 12
  • 28
  • 1
    Ehrm. doing like what you posted here would lead to a stack overflow if you don't override the method. Create a method that is called from the `testMethod` en implement that, don't override methods. The same is done in several classes of the spring framework itself. – M. Deinum May 26 '15 at 10:59

1 Answers1

1

If you don't need specific behaviour from the child you can simply not override it. If you don't have textMethod in child, the parent's method will probably be used.

If you have specific behaviour to add to your child then add the @Override annotation and call super.testMethod() whenever you need it.

RPresle
  • 2,436
  • 3
  • 24
  • 28