McRaven
McRaven

Reputation: 111

Class inheritance in XTEXT type rule

Lets assume that I have to model an abstract class "Car" with a cross reference to an Owner (class name is "Owner") and lets say that I also have to model three concrete subclasses of "Car" called "SUV", "Limousine" and "Sportscar" with some additional specific properties.

Is it possible to model such an inheritance in XTEXT ?... e.g. like:

abstract Car:
   owner=[Owner]
;

SUV extends Car:
   SuvDetails=ID
   ...
;

Limousine extends Car:
   LimousineDetails=ID
   ...
;
etc...

Or do I have to write the line "owner=[Owner]" from "Car" in each subclass (and probably even do not define the abstract class car)?

Or how do you usually model abstract classes, inheritance and similar concepts in XTEXT?

Kind regards and thanks a lot ! :-)

Upvotes: 1

Views: 1075

Answers (1)

Gaetan
Gaetan

Reputation: 101

in fact if you want to make an inheritance you need to define your own metamodel and import it inside your grammar.

It's not possible to make an inheritance between your rules. But you can do like this :

Car:
   (({SUV} 'suv') | 
    ({Limousine} 'limo')) owner=[Owner] details=ID
;

Xtext will create an EClass Car, SUV and Limousine. SUV and Limousine will have an inheritance from Car. When the rule 'Car' will be apply the EClass SUV or Limousine will be instanciate.

Upvotes: 0

Related Questions