#TechForNewbies: Inheritance

Maggie Hunt
4 min read6 hours ago

--

One of the four pillars of Object-Oriented Programming (OOP), inheritance works like genetics… kind of.

Disclaimer: I am not a geneticist.

Inheritance means classes in your code can ‘inherit’ elements (properties as well as methods) from ‘parent’ classes. They can then have their own functionality and features, too, which the parent classes do not necessarily have access to.

Think of it like this:

Jimmy inherited his wonky teeth and wonderful singing voice from his father, Alan. He is also a mastermind computer whiz. In contrast, Alan has to call in tech support whenever he gets locked out of his computer — he never was one for numbers or new-fangled machines.

Jimmy and his partner Alex — a professional gymnast with naturally perfect teeth— have a baby daughter called Gail. She is likely to have inherited both Alan’s talent for singing, and Jimmy’s technical wizardry. She is also likely to have Alex’s flexibility.

At some point — we will also find out whether she has inherited Jimmy or Alex’s teeth!

That is (kind of) how inheritance works in OOP, too.

Parent and Child classes

The class ‘inheriting’ things is referred to as the ‘child’, and the class that the child inherits from is referred to as the ‘parent’.

Now, inheritance doesn’t mean a class is limited to the ‘parent’ class’s functionality. It just means that the ‘child’ can only ‘inherit’ functionality (i.e. use it, without having to be written again) from ‘parent classes’. It can also inherit from the parent class of its parent class… and any other class in the whole ‘inheritance chain’! So Gail can inherit from Jimmy, and also from Alan.

A key thing to remember is that inheritance only works one way! Alan can’t inherit tech skills from his son Jimmy, and Jimmy can’t inherit better teeth from his daughter Gail.

Single vs. Multiple Inheritance

Different programming languages support different kinds of inheritance; some — like Java — support Single Inheritance exclusively, and some — like Python — also support Multiple Inheritance.

Single Inheritance is an instance of a class inheriting things from only one other class. For example, imagine Jimmy is actually a clone of Alan, so the only things he can inherit (instead of develop himself) come directly from Alan’s own skills, features and so on. There was never any chance of him having good teeth, or a bad singing voice, from some other parent.

Multiple Inheritance means the class can inherit things from more than one other class. In case of multiple inheritance, the child will have multiple parents! This is how Gail has many things she could inherit from Jimmy, Alan, or Alex.

Working with Inheritance

Inheritance is a very helpful facet of programming, which allows us to save time and effort (both when writing code and running it)! It means that it is very easy to reuse code within ‘lower’ parts of the programme.

That said, you do have to be aware that it comes with its challenges.

Consider someone looking at Gail at a family gathering, one day, and saying, ‘well, I can see where you get your smile from!’ Are they complimenting her? Or are they being rude and telling us that she has inherited Alan’s bad teeth from Jimmy? The same confusion will apply if you are not careful when working with ‘multiple inheritance’ in your code.

class Alan:
def __init__(self):
self.singing = "great voice"
self.teeth = "wonky"

class Jimmy(Alan): #Jimmy inherits from Alan, so we don't need to set self.singing
def __init__(self):
super().__init__() #initialises upwards inheritance chain, which in this case is just Alan
self.tech_skills = "wizard"

class Alex:
def __init__(self):
self.teeth = "perfect"
self.flexibility = "flexible"

class Gail(Jimmy, Alex): # Gail inherits from both Jimmy and Alex
def __init__(self):
super().__init__() #initialises the whole upwards inheritance chain, so that Gail can access Jimmy, Alex and Alan's features to inherit
self.age = "baby"

jimmy = Jimmy()
alex = Alex()
gail = Gail()

print(jimmy.singing) # "great voice"
print(jimmy.teeth) # "wonky"
print(alex.teeth) # "perfect"
print(alex.flexibility) # "flexible"

print(gail.singing) # "great voice"
print(gail.flexibility) # "flexible"
print(gail.teeth) # but... which teeth does she have? Whose smile has she inherited?

If variables have the same name as each other, you could find yourself with mess to unravel. Some languages will make the decision for you (which will be confusing down the line), others will simply error and your code will break. To avoid this, you can specify which class the element has been inherited from, e.g.

class Gail(Jimmy, Alex):
def __init__(self):
super().__init__()
self.age = "baby"
self.teeth = Alex.teeth # Gail has explicity inherited Alex's teeth

jimmy = Jimmy()
alex = Alex()
gail = Gail()

print(gail.teeth) # "perfect" - good news!

To avoid your code behaving unexpectedly, it’s best to ensure that your classes don’t use the same variable names as each other unless you’re intentionally overriding them in the child class…

class Gail(Jimmy, Alex): 
def __init__(self):
super().__init__()
self.age = "baby"
self.teeth = "rotten" #overriding all inherited possible teeth variables

jimmy = Jimmy()
alex = Alex()
gail = Gail()

print(gail.teeth) # "rotten" - Gail has overridden her genetic predisposition to either perfect or wonky teeth, and they now reflect her lifestyle rather than genetics!

Working with Inheritance is very powerful when well-managed; you just need to make sure you keep track of your variables and manage your structure thoughtfully, expecially when working with multiple inheritance. It can get complex if you don’t!

Photo by Max Ravier: https://www.pexels.com/photo/portrait-photography-of-two-person-s-brown-and-blue-eyes-3396959/

--

--

Maggie Hunt
Maggie Hunt

Written by Maggie Hunt

Software Engineer, #SustainableIT advocate, neurodivergent nerd

No responses yet