Expand my Community achievements bar.

Why do we extend interfaces of core components?

Avatar

Level 2

I’m relatively new to AEM development. I have recently had to extend one of the core components. Why is it that we extend the interfaces that those components implement, rather than extending the class itself, so that our child class has direct access to all the parents’ methods?

 

From my understanding, in order to access the parent component’s class methods, we have to instantiate the parent class as a field in the child class with @Self and @Via, and mark it as a superclass. Why can we not just extend the parent class like any other java parent-child relationship? 

For example, to extend the Core Form Options component, I had to create a child class that implements the Option interface instead of extending the  OptionsImpl class.  Then, I had to have a field within my child class that holds the parent component in order to access its public methods, instead of calling directly from the child. Why is that?

 

 

1 Reply

Avatar

Community Advisor

Hello @dms879090 

 

The answer lies in the basic difference between between Extend and Delegation.

You would be able to find many articles that explain them with examples.

 

Just to summarize:

Extending a class in Java is a way of inheriting and reusing its functionality, while using the delegate pattern is a way of adding new functionality or providing a new way of implementing existing functionality without changing the behavior of an existing class.

 

In case of WCM core components, we generally add a new functionality or change a way its implemented. Which exactly matches with the purpose of delegate pattern


Aanchal Sikka