7 things to know about what Swift allows with inheritance.

Good practices

Before we start, here are some good practices from Code Complete:

  • Child class should be a specialized version of the parent class (think about the L from SOLID principle, the Liskov substitution principle)
  • Child class should not redefine a private member from its parent class. Some languages allow it. (see our point number 7 below 🥲)
  • It’s better to keep parent class properties private and if necessary give getter/setter for child class to use.
  • If there’s only one child class from a parent class, maybe you’re anticipating the future too much and creating useless complexity.
  • Child class should not be created to remove functionality from the parent class.
  • Limit yourself to 3 or 4 levels of inheritance.
  • Some languages allow multi-inheritance, always use with care. Swift allows it.

Inheritance in Swift

Now, time for inheritance in Swift:

  1. Private members of parent class are inaccessible in child class

--

--

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