Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

what is difference between @deligation and Extend

Avatar

Level 3

providing any use would appreciated!

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Anilkumar9 ,

 

Please read through this blog where delegating the OOTB AEM Components are precisely explained.

https://kiransg.com/2021/11/07/aem-core-component-delegation/

View solution in original post

3 Replies

Avatar

Community Advisor

Hi

Let me give you some input here.

 

1. Inheritance using `extends`: In Java `extends` keyword is used to indicate that a class is inheriting from another class. Inheritance is an "is-a" relationship where a subclass inherits the properties and behavior of the superclass. The subclass can extend the functionality of the superclass by adding new methods and fields or by overriding existing ones. Java supports single inheritance, meaning a class can only inherit from one superclass.

Example:

class SuperClass {
// some fields and methods
}

class SubClass extends SuperClass {
// additional fields and methods
}

 

2. Delegation: Delegation is a design pattern where an object passes on a method call to another object, known as the delegate. The delegate performs the actual task, and the delegating object controls the flow of the program. Delegation allows for composition and provides more flexibility compared to inheritance.

class DelegatingClass {
private Delegate delegate;

public void delegatedMethod() {
// delegate to the delegate object
delegate.delegatedMethod();
}
}

class Delegate {
public void delegatedMethod() {
// do something
}
}

 

In short, `extends` is used for achieving inheritance in Java, while delegation is a broader concept involving design patterns and composition to distribute responsibilities among different objects.

Hope this helps!



Esteban Bustamante

Avatar

Correct answer by
Community Advisor

@Anilkumar9 ,

 

Please read through this blog where delegating the OOTB AEM Components are precisely explained.

https://kiransg.com/2021/11/07/aem-core-component-delegation/

Avatar

Administrator

@Anilkumar9 

In Adobe AEM, both @Delegate and @Extend annotations are used to enhance the functionality of existing components. However, they serve distinct purposes and are employed in different scenarios.

@Delegate:

The @Delegate annotation is employed for leveraging the features of an existing component within a new component. It enables the new component to access and utilize the properties, methods, and functionalities of the existing component. This approach is particularly useful when you want to incorporate the behavior of a core component into a custom component without modifying the core component itself.

@Extend:

The @Extend annotation is utilized for extending the JSON model of an existing core component. It allows developers to add new properties and content to the existing component's JSON representation, thereby expanding its capabilities. This technique is particularly beneficial when integrating the AEM SPA Editor with React applications.

Key Differences:

  • Purpose: @Delegate focuses on reusing existing component functionality, while @Extend focuses on expanding the JSON model of a component.

  • Scope: @Delegate is employed within the Sling Model layer, while @Extend is applied to the JSON model of a component.

  • Usage: @Delegate is used to delegate methods and properties from an existing component, while @Extend is used to add new properties and content to the JSON model.

Examples:

  • @Delegate: Suppose you have a core component called "Image" and you want to create a custom component called "EnhancedImage" that inherits the image display functionality of the core component but adds additional features. You can use @Delegate to access the properties and methods of the "Image" component within the "EnhancedImage" component.

  • @Extend: Consider a scenario where you want to extend the JSON model of the "Image" component to include a new property called "caption" that holds the image caption text. Using @Extend, you can add this new property to the JSON representation of the "Image" component, making it available for use in your application.



Kautuk Sahni