Swift Inheritance - Swift Programming

What is Swift - Inheritance?

Inheritance is the ability to take more than form. Usually, a class can inherit methods, functionalities, and properties from other class. Classes can be further divided in to sub class and super class.

  • Sub Class − when a class inherits properties, methods and functions from other class then it is called as a sub class.
  • Super Class − Class containing properties, methods and functions to inherit other classes from itself then it is called as a super class.

Swift classes consists of super class that calls and access methods, properties, functions and overriding methods. Also, property observers are also employed for adding a property and for altering the stored or computed property methods.

What is a Base Class?

A Class which does not inherit methods, properties or functions from other class is known as a 'Base Class'.

When we run the above program with playground, we will get the below ouput −

Class with class name StudDetails are defined as a base class here which is employed to contain students name, and three subjects mark as mark1, mark2 and mark3. The keyword 'let' is used for initializing the value for the base class and base class value is shown in the playground with the use of 'println' function.

What is a Subclass?

The act of basing a new class on an existing class is described as 'Subclass'. The subclass will inherit the properties, methods and functions of respective base class. To define a subclass ':' is employed in prior to the base class name.

When we run the above program with playground, we will get the below output −

Class 'StudDetails' is defined as super class where student marks can be declared and the subclass 'display' is employed for inheriting the marks from its super class. Sub class will define students marks and calls the print() method for displaying the marks of the students.

What is Overriding?

The process of accessing the instance, type methods, type properties of super class is called Overriding. The keyword 'override' is used for overriding the methods in the super class.

Access to Super class Methods, Properties and Subscripts

The Keyword 'super' employed used as a prefix for accessing the methods, properties and subscripts that are declared in the super class.


Access to methods, properties and subscripts







Methods Overriding

Inherited instance and type methods can be overridded by the 'override' keyword to our methods that are defined in our subclass. Here, print() is overridden in subclass for accessing the type property mentioned in the super class print(). Also, new instance of cricket() super class is created as 'cricinstance'.

When we run the above program with playground, we will get the below output −

Property Overriding

One can override an inherited instance or class property in order to facilitate your own custom getter and setter for that property, or for adding property observers for enabling the overriding property in order to notice when the underlying property value alters.

Overriding Property Getters and Setters

Swift permits the user to facilitate custom getter and setter to override the inherited property if it is a stored or computed property. The subclass don't know the name and type of inherited property. Therefore, it is necessary that the user should specify in subclass, the name and type of the overriding property mentioned in super class.

This can be done in two different ways −

  • When setter is defined for overriding property the user has to define getter too.
  • When we don't wish to alter the inherited property getter, we can just pass the inherited value by the syntax 'super.someProperty' to the super class.

When we run the above program with playground, we will get the below output −

Overriding Property Observers

The 'property overriding' concept comes in to picture in Swift when a new property requires to be added for an inherited property, This notifies the user when the inherited property value is changed. But overriding cannot be applied for inherited constant stored properties and inherited read-only computed properties.

When we run the above program with playground, we will get the below output −

Final Property to prevent Overriding

Swift introduces 'final' property to prevent overriding, when the user don't wants others to access super class methods, properties or subscripts. The subscripts won't permit the super class methods, properties and its subscripts to be overridden once after the 'final' property is declared. There is no provision to have 'final' property in 'super class'. When the 'final' property has been declared the user is confined to create further sub classes.

When we run the above program with playground, we will get the below output −

As the super class is declared as 'final' and its data types are also declared as 'final' the program won't permit for creating subclasses further and it will raise errors.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Swift Programming Topics