Swift Inheritance

In this tutorial you will learn about the Swift Inheritance and its application with practical example.

Swift Inheritance

The inheritance is one of the important feature in object oriented programming, that facilitates the code reusability. Inheritance is a mechanism through which a class(derived class) can inherit properties and methods from an existing class(base class).

Sub Class:- The class that inherits properties and methods from another class is know as Sub-class or Derived-Class.

Super Class:- The class whose properties and methods are being inherited by sub class is called Base Class or Super class.

Swift Base Class

The class whose properties and methods are being inherited by sub class is called Base Class or Super class.In swift any class that is not being inherit from any other class is known as a base class.In swift, there is no universal base class, any classes you define without specifying a superclass automatically become base classes.

In Swift, class can be defined using the class keyword as following –

Syntax:-

Here, className is replaced with the actual class name then in between the curly brackets {} we define all the associated class properties and methods.

Example:-

Output:-

swift_inheritance_baseclass

Swift Sub Class

The class that inherits properties and methods from another class is known as Sub-class or Derived-Class.In swift subclass are also allowed to override the properties or methods of base class. In addition to the inherited properties and methods a sub class can also have their own properties and methods.

In swift, a subclass can be defined using subclass name before the superclass name, separated by a colon.

Syntax:-

Example:-

Output:-

swift_inheritance_subclass

Swift Overriding

In swift, a subclass is allowed to customize an instance method, type method, instance property, type property, or subscript as per requirement, this feature is known as overriding in swift.

Swift Method Overriding

We have seen using inheritance method defined in a baseclass is inherited by its subclass and is used by subclass instances. However, we may want an instance to respond behave differently for the same method defined in baseclass. This can be achieved by method overriding. In method overriding we are allowed to define a method in subclass with same name, parameter list and return type. Then, when that method is called, method defined in the subclass is invoked instead of the one defined in the baseclass.

In swift, a baseclass methods can be overridden in subclass by the “override” keyword followed by method definition with same name, parameter list and return type as in baseclass.

Example:-

Output:-

swift_method_overriding

Swift Overriding Properties

Inherited properties of a baseclass can be overridden in subclass to provide own custom getter and setter for that specific property, or to add property observers to overriding property to observe when that property value changes.

Example:-

Output:-

swift_overriding_properties

Swift Overriding Property Observers

In swift, property observer can be added to an inherited property as property overriding. Adding property observer allows us to be notified when the value of an inherited property changes.

Example:-

Output:-

swift_overriding_property_observer

Swift Prevent Overriding

In swift, all of the methods and properties can be overridden in subclass by default. If we want to prevent a method, property, or subscript from being overridden in subclasses, we can declare them as final using final keyword. Making a method or property final ensures that they won’t be altered in subclasses. Similarly if we want to prevent a class being inherited or subclassed further then we can make it final using final keyword.

In this tutorial we have learn about the Swift Inheritance and its application with practical example. I hope you will like this tutorial.