Inheritance And Overriding In Swift

Introduction

 
A class can inherit methods, properties, and characteristics of another class. When one class inherits from another, the inheriting class is a subclass, and the class it inherits from is a superclass. Classes in Swift call and access methods, properties, and subscripts belonging to superclass. They then provide their own overriding versions of methods, properties, and subscripts to refine or modify behavior. 
 

Base Class

 
Any class that does not inherit from another class is known as a base class. Swift classes do not inherit from a universal base class. The class which is defined without specifying the superclass automatically becomes a base class. The below base class has two properties as numberOfWheels and maxPassengers. These properties are used by methods as a description which returns a String description.
  1. class Vehicle    
  2. {    
  3.  var numberOfWheels : Int    
  4.  var maxPassengers : Int    
  5.  func description () - > String    
  6.   return "\(numberOfWheels) wheels; up to \(maxPassengers) passengers"    
  7. }    
  8.  init ()    
  9. {    
  10.   numberOfWheels = 0    
  11.   maPassengers = 1    
  12. }    
The vehicle class defines an initializer to set up its properties. Initializers are used to create a new instance of a type. Initializers are not methods and it prepares a new instance for use, and ensure all properties of instance have valid initial values. 
 

Subclassing

 
Subclassing is the act of basing a new class on an existing class. The subclass inherits characteristics from the existing class. The new characteristics can be added to the subclass. Write the superclass name after the original class name to indicate that a class has a superclass. In the below example, Bicycle is a subclass of vehicle and vehicle is the superclass of the bicycle. The new Bicycle class automatically gains all characteristics of the vehicle as its masPassengers and numberOfWheel properties.
  1. class  Bicycle : Vehicle    
  2. {    
  3.  init()    
  4. {    
  5.  super : init()    
  6.  numberOfWheels = 2    
  7. }    
  8. }    
Subclasses are only allowed to modify variable properties of the superclass during initialization. The inherited constant properties of subclasses cannot be modified. The below example creates a subclass of Bicycle for two-seater bicycle is a tandem and it inherits the two properties from Bicycle and It inherits these properties from the vehicle.
  1. class Tandem : Bicycle  
  2. {  
  3.  init()  
  4. {  
  5.  super.init()  
  6.  maxPassengers = 2  
  7. }  
  8. }  

Overriding

 
A subclass can provide its own custom implementation of an instance method, class method, instance property, or subscript that it would otherwise inherit from a superclass.
 
To override a characteristic that would otherwise be inherited prefix the overriding definition with the override keyword. Overriding without override keyword cause error when the code is compiled. The override keyword prompts the Swift compiler to check the overriding class superclass has a declaration that matches the one provided for override.
 
An overridden method calls the superclass version by calling the method within the overriding method implementation.
 
An overridden property can access the superclass version within the overriding getter or setter implementation.
 
An overridden subscript can access the superclass version of the same subscript within the overriding subscript implementation.
  1. class Car : Vehicle  
  2. {  
  3.  var speed : Double = 0.0  
  4.  init()  
  5.  {  
  6.    super.init ()  
  7.    maxPassengers = 5  
  8.    numberOfWheels = 4  
  9.  }   
  10.   override func description () - > String  
  11.  {  
  12.   return super.description () + ";" + "travelling at \(speed) mph"  
  13. }  
  14. }  
Car overrides its inherited description method by providing a method with the same declaration as a description method from the vehicle. The overriding method definition is prefixed with the override keyword.
 

Getter and Setter

 
The custom getter can be provided to override any inherited property, whether the inherited property is implemented as a stored or computed property as its source. The stored nature of inherited property is not a subclass and it only knows the inherited property has a certain name and type. 
  1. class SpeedLimitedCar : Car  
  2. {  
  3.  override var speed : Double  
  4. {  
  5.  get  
  6. {  
  7.   return super.speed  
  8. }  
  9. set  
  10. {  
  11.  super.speed = min (newValue, 40.0)  
  12. }  
  13. }  
  14. }  

Preventing Overrides

 
The prevention of method, property, or subscript from overriding by marking it as final. It is implemented by the @final attribute before its introducer keyword. Any attempt to override a final method, property, or subscript in a subclass is reported as a compile-time error. Methods, properties, or subscript that add to a class in an extension can be marked as final within the extension's definition.