Crystals Declassified Interview Survival Guide

All of the alum I have talked to during my time at Flatiron have not only emphasized on how important it is to do algorithms, but they have also noted what areas of focus a programmer who is interviewing at a tech company should have. Some of these topics that interviewers will ask include the following: prototype chain, composition over inheritance, em, rem, Prototypal Inheritance, the pros and cons to object oriented programming, design patterns, etc.

In lieu of these topics, I will be creating a series denoting the messages the Flatiron alum have told me and would want me, a future programmer, to know and how to best prepare for those coding interviews.

For todays topic, I will be writing about composition versus inheritance.

Why inheritance is dangerous

Inheritance is the idea of your child class inheriting behavior from the parent class. While this is what we’ve been taught, it could also be malicious to your code. For the idea of inheriting behavioral information from a parent class sounds benign, if behavior changes in the parent class, it affects the child class or the inheritors as well. This is clearly reckless and can break your code. To avoid breaking your code, start looking into [“interfaces that define contracts between classes, create final classes to implement behavior for those interfaces and use composition to prevent complexity”](Pignatelli, 2018).

Before I go in depth what composition is, I want to take the time to define separately what composition and inheritance are. In object oriented programming, inheritance is the idea that a subclass will model behavior from the superclass or parent class. Whereas with composition, it is the idea that a class would reference one or more objects from other classes through instance variables that extends to any method or function; composition “defines a class as the sum of its parts”. Not only does composition reject dependency on another class, think of it as creating small functions (Damcosset). Another way to put it is, inheritance is a ‘is-a’ relationship. Whereas composition is a ‘has-a’ relationship.

class Automobile {} // Automobile class is a parent to the Car classclass Car extends Automobile { // Car is an Automobile, so Car class extends Automobile class.
private Engine engine;
// Car has an Engine so, Car class has an instance of Engine class as its member.
}

In this example, the car has an engine, whereas the car is a automobile

Why Composition?

Think of composition like a lego car; this lego car is the sum of its parts.

To rephrase with the example above, composition is the use of instance variables in reference to other objects. A car is a type of automobile, which means a car extends the automobile class… signifying that a car IS-A automobile. A car class uses the private engine method through composition, denoting that a car HAS-A engine.

Composition aligns with the idea of polymorphic behavior — to complete a task through many forms or ways. It is the idea that allows programmers to write code that extends itself throughout the entirety of the code, through methods and functions; when programmers want to reuse code to manage functionality of the program itself, composition takes many forms for one function or method. While with inheritance it can be dangerous to change your code later on without affecting the child class, with composition, it takes the burden off of your shoulders from worrying about breaking your code.

Composition allows a programmer to change behavior without worry of class dependency.

Conclusion

While Inheritance has been taught for many years on sharing behavior throughout your code from a parent to child class, composition is a worthy discussion regarding how to not only be a better programmer but to implement better coding practices as well. Through composition, we risk less. Through inheritance, we jeopardize all the long hours and nights of coding and creating a project. It is worth noting that developing programmers should not only understand the differences between the two but to learn how to code both.

Additional resources to look at:

About Thorben JanssenThorben is an independent trainer and author of the Amazon bestselling book Hibernate Tips — More than 70 solutions to common Hibernate problems.He writes about Java EE related topics on his blog Thoughts on Java. “OOP Concepts for Beginners: What Is Composition?” Stackify, 28 Apr. 2020, stackify.com/oop-concepts-composition/.

Brian. “Inheritance vs. Composition.” Medium, 10 May 2020, medium.com/better-programming/inheritance-vs-composition-2fa0cdd2f939.

Damcosset. “Coding Games and Programming Challenges to Code Better.” CodinGame, www.codingame.com/playgrounds/6485/exploring-composition-in-javascript.

“Inheritance (IS-A) vs. Composition (HAS-A) Relationship.” w3resource, 26 Feb. 2020, www.w3resource.com/java-tutorial/inheritance-composition-relationship.php.

Pignatelli, Nicolo. “Inheritance Is Evil. Stop Using It.” Medium, 4 Jan. 2018, codeburst.io/inheritance-is-evil-stop-using-it-6c4f1caf5117.

Vats, Rohan. “Polymorphism vs. Inheritance: Difference Between Polymorphism & Inheritance [2021].” UpGrad Blog, 11 Jan. 2021, www.upgrad.com/blog/polymorphism-vs-inheritance/#Polymorphism.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store