Inheriting or Inclusion, the differences between both
Many programming languages make use of inheritance and inclusion. For beginner programmers, this can be a daunting concept but once understood, they can open up a world of new possibilities for your software development. This post is aimed at those who have an understanding of programming, but perhaps need or want a little example of inheritance VS inclusion with an easy to follow example.
What is inheritance?
Inheritance is the act of developing upon a base case and modifying it for the needs of the new class. Inheritance is used in the same sense as genetics in humans. All humans have a mother and a father from which they inherit certain traits. The child then makes new traits which are in turn passed on to their children. Inheritance in programming works in the same way. A base class (often referred to as the parent class) can pass traits on to the derived class (often referred to as the child class). Inheritance can be used to join together several similar classes with a common function to give them a common parent. This is used to help keep code clean and maintainable (as well as the other benefits of inheritance).
What is inclusion?
Inclusion is similar to inheritance in that it allows other classes to borrow certain traits (behaviours) for their own need. The real-world simile for this is friendship, whereby one person has something a second needs and can make use of it through friendship with the first person. If you take James (a fellow EngDoc researcher) and I, we both have our own traits and behaviours and are two different people doing different things. However, in our work, James makes use of C# development in Grasshopper (a plethora of such work can be seen here) and I make use of parametric modelling.
However, my expertise is in programming while James is the parametric guy. Therefore, as friends, we share our traits among each other to assist both of us in our day-to-day workings where I enrich James’ abilities in C# development and he assists me in parametric modelling. However, the key thing here is that the traits remain ours. So I remain the software ‘expert’ to James, and he remains the parametric modelling ‘expert’ to me (of course, in real-life we both learn from each other so that we can do the work without always asking around, but for the purpose of this simile, assume we forget everything after being told if for the purpose we need it).
Inclusion works in a similar fashion in programming. The traits (behaviours) shared amongst classes do not belong to the class including them, but they may make use of them to make their work easier.
The difference between inheriting and including
The biggest difference between inheritance and inclusion is what the derived (child) class is able to do with the behaviour coming from the parent or friend class. In proper programming practice (which of course everyone always adheres to), you make your variables private (anecdote – I have seen code where variables were made public with the comment “//Code didn’t compile when these were private so made them public and it works” – lesson for all, don’t let non-programmers near your code!), some methods private and some methods public. The class which owns these methods and variables can, naturally, use them in any way they see fit.
Classes which inherit from a parent class can also ‘see’ and use any method and variable that belongs to the parent class. So a private variable in the parent class is directly accessible in the child class. This is because the child class has inherited this variable, so while you do not need to declare it in the child class, the child class can make use of it from its declaration in the parent class.
Classes which include other classes can only access and make use of the public methods (and variables if you’re silly enough to make your variables public). Anything which is private is hidden from the including class. In our simile, this is where the private intricate programming knowledge is kept hidden by me from James, and only the public ‘methods’ (i.e. asking for help) are accessible to him. Classes which include other classes can still obtain the data held by private variables or change them through the use of get and set methods, but access directly to private functions is prohibited.
You can choose to use inheritance after you have built your classes (though this increases development time – ideally you would plan your classes out beforehand and already know what inheritance you were using) by going through classes which have similar variables and behaviours and putting any common traits into a parent class from which the originals can then inherit. This makes for better maintainability in code, as a fix to a method needs only be done once, as opposed to being done across several files (and running the risk of forgetting one).
Class type | Private | Public |
First class (owns the methods, etc.) | Yes | Yes |
Inheriting Class | Yes | Yes |
Including Class | No | Yes |
Another key thing to note is who owns the data when you are using it. In inheritance, the derived (child) class owns the data, whereas in inclusion, the original class owns the data. This is because inclusion is primarily for Object Orientated Programming (OOP) and the class including the original may want to make several instances of the class (several objects) with slightly different attributes (for example, several cars but with different numbers of wheels or doors). Inheritance however is used to extend functionality and group common functionality in one parent class.
Hopefully this post makes sense, and the difference between simply including classes and inheriting from them is clear (or clearer if you were looking for an explanation). If something doesn’t make sense feel free to leave a comment or contact me and I’ll try to help make sense of it.