Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

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.

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".

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

Add your "MyBaseClass".

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

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