Akshat
Akshat

Reputation: 575

Call Parent class PostConstruct method instead of child class

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

Upvotes: 1

Views: 1006

Answers (1)

RPresle
RPresle

Reputation: 2561

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.

Upvotes: 1

Related Questions