9 Inheritance

WHAT YOU WILL LEARN IN THIS CHAPTER:                  

  • What inheritance is
  • How to define and instantiate a base class
  • How to create an abstract class
  • How to inherit a base class
  • How to override methods
  • How to override initializers
  • How to overload initializers
  • How to create abstract methods
  • How to overload methods
  • How to prevent subclassing
  • The different types of initializers
  • How to perform initializer chaining
  • How to call initializers in subclasses
  • Using extensions in Swift
  • Using access controls in Swift

In the previous chapter, you learned how classes are defined and how to add methods and properties to a class. In this chapter, you continue to explore object-oriented programming (OOP) by looking at another important topic—inheritance. In addition, you will also learn about access controls and how Swift’s interpretation of access control is different from conventional programming languages.

UNDERSTANDING INHERITANCE

Class inheritance is one of the cornerstones of OOP. It basically means that a class can inherit the properties and methods from another class. Class inheritance enables a high degree of code reuse, allowing the same implementation to be adapted for another use. Swift fully supports the capability of class inheritance.

Get Beginning Swift Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.