Shury
Shury

Reputation: 578

Class inclusion vs Class Inheritance

I've found a question asking about the differences between class inclusion and class inheritance. What exactly does class inclusion mean? I don't think I've hear this term before ..

Upvotes: 1

Views: 912

Answers (3)

J_S
J_S

Reputation: 3282

Think about your problem in terms of "is-a" and "has-a" (composition and inheritance). Please take a look at: Is-a and Has-a.

In short terms - if the class/object you're trying to implement "is" an instance of something more general, then it is an example of inheritance i.e.: Apple (derived class) is a Fruit (base class), Porsche is a Car etc.

If your object "has" or "is part of" something, then it is composition. Example: Car has an Engine (object of Engine is included in every Car object), House has Windows and Doors etc.

Whether you should use one or another is a topic for a different type of discussion. In general you want to prefer composition over inheritance as this allows for more flexible code.

Upvotes: 2

asavinov
asavinov

Reputation: 41

Inclusion may have different treatments depending on the context:

  • InstanceOf/TypeOf relation describe the relationship between an instance and its class. In mathematics, it is membership relation (a member is included in its set). In the relational model, it is how tuples are members of relations.
  • General-specific relation among classes. In mathematics, it is subset relation which means that all members of a more specific set are also members of its more general set.
  • Object inclusion in prototype-based programming (like JavaScript). Here an object is included in its prototype object. Note however that it is not inclusion of classes and inheritance is implemented differently.
  • Concept inclusion in concept-oriented programming where concepts generalize classes and inclusion generalizes inheritance. In particularly, objects can share a super-object precisely as classes can share a super-class.
  • Composition. It is actually a different relation - not inclusion. Yet, it can be treated as inclusion under this assumption: composition object/class is a more specific element than an element it has. This assumption is one of the features of the concept-oriented model. It is a quite natural assumption which allows us to use composition for inheriting. One its advantage is that a super-object may have many extensions.
  • Containment relation. It is normally used to describe how instances are included in container objects (like Bad, Set etc.)
  • Inner classes.

One of the main conceptual differences of all forms of inclusion from inheritance is that inclusion assumes that many elements can be included in one (parent) element at the level of instances. In contrast, the classical inheritance (implicitly) assumes that an object may have only one extension. In other words, a super-object cannot be shared among many extensions.

Note again that it is probably not a complete list and there are significant overlaps between the above treatments as well as some incompatibilities.

Upvotes: 0

Jens
Jens

Reputation: 9416

It's composition. Generally speaking, prefer composition over inheritance. Use inheritance to model only is-a-relationship (Liskov principle), not uses, dependencies or implemented-in-terms-of.

Upvotes: 2

Related Questions