0
votes

In AEM, I have a Use class A which extends WCMUsePojo. It has one activate() method with @Override annotation where I read the property (lets say product) and assign to variable. Also, I have a getter method to read the property. Now, there is another class B which extends the Class A and has activate() method with @Override annotation. In activate method I am reading one more property.

Now, from HTL , I refer the Class B, and was trying to get "product property" (assuming that this property would be available in Class B via inheritance), But I am getting null value. But when I change the property modifier to static in Class A, then it works fine.

See the code below.

public class ClassA extends WCMUsePojo {  
    private String product;
 
    @Override   
    public void activate() throws Exception {    
        product = getProperties().get(“product”, "");
    }
  
    public String getProduct() {    
        return product;  
    }
}

public class ClassB extends ClassA {  
    private String lotno;
 
    @Override   
    public void activate() throws Exception {    
        lotno = getProperties().get(“lotno”, "");
    }
  
    public String getLotno() {    
        return lotno;  
    }
}


<div data-sly-use.productDetails="test.sample.ClassB"/>
${productDetails.product} 

${productDetails.product} is null unless I change the product property to static in ClassA. Can somebody explain why is that so?

1

1 Answers

0
votes

Just add super.activate() in your activate-method of class B.

This is Standard-Java behavior. An overridden method replaces the inherited method. The child-class has to call super.method-xyz() to re-use it. So the child class can control if the inherited method is called and when.


Additional remarks:

  1. In case of an constructor you must call super(), because also the super-class must be initialized.

  2. If you should use Sling-Models (what is mostly recommended), then you have the same effect. The annotation @PostConstruct is also only used for the child class. This is more confusing for everybody. That's why I don't recommend inheritance for Sling-Models in my projects. Often it is not needed.