-
Notifications
You must be signed in to change notification settings - Fork 13
Inheritance
All children inherit genes from their biological parents. It is not yet understood exactly which genes are inherited from which parent but we know for sure that all genes of a child come from their parents. Similarly, in conceptual modeling concepts can inherit elements from other concepts. When clafer A inherits elements of clafer B we say that:
- clafer A extends clafer B,
- A is a subclafer of B,
- B is a superclafer of A.
In contrast with the biological inheritance, concepts always inherit everything from given superclafers. When a clafer has only one superclafer, we call that single inheritance. If there are more superclafers, we call that multiple inheritance. If a clafer has no superclafers, then it does not inherit anything. Inheritance in Clafer serves for several purposes.
Abstract clafers generalize some concepts in a given domain. We agreed previously that, abstractly speaking, car is a thing with engine:
abstract car
engine
Such a definition of car is very generic and applies to any car. It does not, however, describe any car in particular. Abstraction helps to categorize similar things and to extract commonalities. Abstract concepts are used in definition of more specialized concepts. Inheritance is a way of reusing abstract clafers.
There are many automotive companies out there. Let us name two manufacturers: Trabant and Hummer. The former produces cars with optional radios:
abstract trabant : car
or radio ?
cdPlayer
mp3
The model communicates that trabant
is a car
. Syntactically inheritance is clafer’s name (trabant) followed by colon and then by name of an abstract clafer (car). Read colon as extends or is a. We define trabant
as an abstract clafer, because we are not talking about any particular model of Trabant. The model says that Trabant is a car, has an engine (inherited from the abstract car
), and has an optional radio.
The second manufacturer, Hummer, offers slightly different cars.
abstract hummer : car
mediaCenter ?
gps
tv
dockingStation
Similarly to Trabant, it inherited engine
, but added mediaCenter
instead of radio
. Hummer is abstract because it does not define any particular model. It serves as a general template for Hummer cars.
An abstract clafer has no instances unless it is extended by a concrete clafer. If we ask the reasoner about configurations of an abstract clafer, it says that there are no configurations. To enumerate configurations of an abstract clafer we extend it by a concrete one.
In the previous section, abstract clafers (trabant
and hummer
) reused another abstract clafer (car
) by extending it and adding new children. Instantiation of abstract clafers is very similar; the only difference is that abstract clafers are extended by concrete ones. Let us encode specific car models of Trabant:
- DeLuxe version
deLuxe : trabant
Trabant DeLuxe extendstrabant
, but does not add any new children. ThedeLuxe
clafer is concrete and we can enumerate its configurations. That way, inheritance allows to instantiate abstract clafers and to obtain their configurations. Trabant DeLuxe represents 4 configurations as the inherited clafers contain variability (due to cardinalities and group cardinalities). - Rally version
rally : trabant turboCharged
Trabant Rally ia a concrete clafer that extendstrabant
, but adds a new child:turboCharged
. It also represents 4 configurations. Each configuration additionally contains theturboCharged
clafer. Inheritance allows to extend abstract clafers with new children and to instantiate them.
Let’s us revisit the initial car example and add cardinality to the car
clafer.
car 200
engine
or radio ?
cdPlayer
mp3
The model represents a pool of 200 cars. It reflects various car models owned by a car rental company. The company has the freedom to configure car radios according to their preferences. At times we need to talk about particular car instances. How to distinguish one car from other 199 cars owned by the company? We use inheritance among two concrete clafers:
ourFirstCar : car
The
ourFirstCar
allows us to configure the first of the 200 cars. Right now ourFirstCar
still represents a car with variability. Later we will use constraints to show how to configure a particular car. We can name also other instances of cars. Let us add another car to the previous model:ourFirstCar : car
superCar : car
In the above model we may refer to
ourFirstCar
and superCar
. Clafer guarantees that both clafers are disjoint, i.e. ourFirstCar
and superCar
are two distinguishable instances. Even though they are distinguishable, they may have the same configurations (e.g. both of them have radios).
The above model talks about 2 cars. What if we wanted to refer to the other 198 cars? It is done in a similar way:
ourFirstCar : car
superCar : car
allOtherCars : car *
The three declarations are guaranteed to be disjoint. The last one covers all cars that are neither
ourFirstCar
nor superCar
. Instead of using the any cardinality, the model could have used 198 as cardinality.
TODO: describe, mention issues
Is inheritance in Clafer overlapping or non-overlapping?
Inheritance in Clafer is non-overlapping (unless it is multiple inheritance. It means that subclafers are disjoint (have different identities) by default. In the example:
abstract car
trabant : car
hummer : car 2
there is one trabant and two hummers. There is no element that is a trabant and a hummer at the same time.
What a subclafer inherits?
TODO: group cardinalities, are clafers optional?
Is it possible to add children to inherited children?
No, currently it is impossible. One can only add new children that are on the same level of nesting as children of the extended clafer. For the example:
abstract car
engine
One may extend
car
by adding radio
:myCar : car
radio
Effectively
myCar
has two children:myCar
engine
radio
It is disallowed, however, to add children to
engine
, such as turboCharged
:myCar : car
engine
turboCharged
We might allow such a construction in the future if there are models that need it. If you need such a language feature now, please contact us. Alternatively, use abstract classes and extend them at a deeper level of nesting.
How is inheritance related to types in Clafer?
Each clafer definition introduces a new type. Inheritance hierarchy is also a hierarchy of subtypes. When a clafer A extends B, then A is a subtype of B.
What is the semantics of inheritance?
It is similar to inheritance in UML and object-oriented programming languages. Theoretically inheritance and subtyping are two separate things (read more on Wikipedia or those lecture notes). Inheritance itself can be thought as a macro for explicitly rewriting inherited clafers. Subtyping allows for a given type to be substituted for another type (is-a relationship). In practice it makes sense to couple inheritance with subtyping. This is what we did in Clafer.
Is it possible to use inheritance at any level of clafer nesting?
Yes, clafers that are deeply nested may extend abstract clafers. This is a perfect Clafer model
myThings
computer
vehicles
motorcycle
myCar : car
One of the vehicles is
myCar
and it extends the abstract car
.
When does inheritance create new instances? What is the meaning of myCar : car
?
Inheritance on its own does not create instances. Depending on types of myCar
and car
, we have the following cases:
-
myCar
is abstract,car
is abstract:
abstract car abstract myCar : car
Such a model has no instances of clafers. -
myCar
is concrete,car
is abstract:
abstract car myCar : car
Such a model has one instance (myCar
). -
myCar
is abstract,car
is concrete. It is an incorrect clafer model. Abstract clafers cannot extend concrete clafers. -
myCar
is concrete,car
is concrete:
car myCar : car
Such a model has one instance that can be refered to ascar
andmyCar
.
So what is the difference between 2) and 4)? The difference occurs if there are more clafers extending car
, such as:
abstract car
myCar : car
anotherCar : car
and
car
myCar : car
anotherCar : car
In the first case, there are two instances of car (
myCar
and anotherCar
). In the second case, there is only one instance that can be referred to as car
, myCar
, and anotherCar
.
When to use cardinalities and when to use inheritance?
TODO: talk about quotation – syntactic sugar
Generative Software Development Lab
University of Waterloo
200 University Avenue West
Waterloo, Ontario, Canada N2L 3G1