Inheritance relations and Polymorphism with "extends" in Akwatype

Inheritance relations and Polymorphism with "extends" in Akwatype

Inheritance and polymorphism are key concepts for structuring and specializing types in a data model. In Akwatype, the "extends" keyword is used to define hierarchical relationships, where a derived type inherits the attributes and relationships of a base type, while being able to add its own characteristics. Inheritance offers a powerful way of organizing entities in a modular way, creating specialized types that share common attributes. This page explores the use of "extends" and the role of polymorphism in building dynamic data models.

To discover our various concepts, click here!

 


 

Inheritance with extends: Specializing and structuring types

Inheritance allows you to create a general-specific type relationship between model entities. By using "extends", a derived type can inherit attributes and relationships defined in a base type, thus promoting the reuse of common characteristics.

  • Entity specialization: The base type represents general characteristics, while derived types add attributes or relationships specific to each specialization. For example, a basic Person type can be extended by Employee and Customer, each with specific attributes (e.g., salary for Employee and purchase history for Customer).

  • Centralization of Common Attributes : Common attributes, such as name and address, are defined in the base type and inherited by derived types, ensuring consistency and reducing duplication of information in the model.

Inheritance facilitates the hierarchical organization of types, allowing the model to be structured to reflect business reality, while simplifying data management.

 


 

Polymorphism and Flexibility in Relationships

Polymorphism is a concept that accompanies inheritance, allowing derived types to be treated as instances of the base type. Thanks to polymorphism, it's possible to create flexible relationships where entities can reference derived types without knowing precisely which one will be used.

  • Reference to Base Type: For example, an author relationship in a Publication can be defined with the Person type as a reference, while still allowing an Employee or Customer instance to be linked, as required.

  • Type interchangeability: Polymorphism makes derived types interchangeable in relations and methods, enabling entities to be manipulated in a generic way without knowing their exact specialization.

Polymorphism increases the flexibility of the model by making derived types compatible with the relations and operations of the base type, thus facilitating integrations and model scalability.

 


 

When to use "extends" rather than "using"?

In Akwatype, "extends" and "using" are two reuse approaches that meet different needs:

  • Use "extends" when you need a hierarchy of specialization between types, and polymorphism is required. For example, for entities sharing common attributes but having important specializations, such as Person being extended into Employee and Customer.

  • Use "using" for common attributes with no hierarchical relationship, especially when polymorphism is not required. For example, if several types such as Customer and Supplier share contact attributes but are not subtypes of a common type.

In summary, "extends" is suitable for specialization with hierarchical and polymorphic requirements, while "using" is preferable for simple centralization of attributes.

 


 

Advantages of Inheritance and Polymorphism with extends

The use of "extends" in Akwatype offers several advantages for building structured and scalable data models:

  1. Redundancy reduction: Inheritance centralizes common attributes in the base type, eliminating duplication of data in derived types.

  2. Modularity and flexibility: polymorphism enables derived types to be treated as instances of the base type, making the model more adaptable to different contexts.

  3. Model scalability: By adding new specializations over time, the model can be extended without disrupting the existing structure.

These advantages make it possible to build data models capable of adapting to changing business needs, offering a solid foundation and a high level of flexibility.



 


 

 

Example of the use of inheritance and polymorphism :

example of inheritance where different types of payment means (Credit Card, Check, Bank Transfer) extend a common base type (MeanOfPayment), allowing an Invoice to use any of these payment means.



 

Akwatype description

 

type Invoice{

   num:String

   amount:Decimal

   meanOfPayment:MeanOfPayment

}

 

abstract MeanOfPayment{

   name:String

   bank:String

   type:String

}

 

type CreditCard extends MeanOfPayment{

    cardNumer:String

    expirationDate:Date

    securityCode:String

    discriminator{

        type: "01"

    }

}

 

type Check extends MeanOfPayment{

    checkNumber:String

    checkingAccountNumber:String

    discriminator{

        type: "02"

    }

}

 

type BankTransfert extends MeanOfPayment{

    eban:String

    creditTransferType:String

    creditTransferObject:String

    discriminator{

        type: "03"

    }

}

 

 

 


 

Conclusion

Inheritance and polymorphism via "extends" in Akwatype enable types to be structured hierarchically, creating modular and scalable specializations. By defining well thought-out inheritance relationships, teams can design data models that meet business needs while remaining adaptable. Thanks to this approach, Akwatype makes entity management flexible, robust and ready for project evolution.

Model your data as early as possible
for effective design.

Structure your data modeling from the start of your projects to communicate, describe your workflows, and empower your development teams.