09. Extending an EMF Ecore model (inheritance)

Optional: 9. Extending an EMF Ecore model (inheritance) #

9.1. Overview #

EMF allows to extend existing models via inheritance. The following will define a base model and an extension based on this base model. This can for example be used to extend the Eclipse e4 application model. It will also demonstrate how to work with EMF ecore models directly without using the ecore tools.

9.2. Example #

  • Create a new EMF project “de.vogella.emf.inheritance”. Create a new model by selecting File > New > Eclipse Modeling Framework > Ecore Model. Name the model "base.ecore". Select "EPackage" as the basis and maintain the following properties for this package.

extend10

  • Right-click on the package and select New Child > EClass. Maintain the class "MyBaseClass" with two "EAttributes" of type "EString". Create a new "Ecore" model "extendedmodel.ecore". Maintain "extended" as the package name. Right-click your model and select "Load resource".

extend40

extend50

extend60

  • Create a new class "MyExtendedClass" and press "ESuperType".

extend70

  • Add your "MyBaseClass".

extend80

extend90

  • Maintain a new EAtribute "detailedField" on "MyExtendedClass".

extend100

  • Create a new genmodel "extended.genmodel" based on extended.ecore. Generated Java code and you will see that the "MyExtendedClass" has extended "MyBaseClass".