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:-
1 2 3 4 |
class className{ // Properties // Methods } |
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class employee { var empName: String = "" var empAge: Int = 0 var empSalary: Int = 0 init(empName: String, empAge: Int, empSalary: Int) { self.empName = empName self.empAge = empAge self.empSalary = empSalary } } var emp = employee(empName: "Keith", empAge: 25, empSalary: 4000) print("W3Adda - Swift Inheritance Baseclass") print("Employee Name is :\(emp.empName)") print("Employee Age is :\(emp.empAge)") print("Employee Salary is :\(emp.empSalary)") |
Output:-
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:-
1 2 3 |
class subclassName: superclassName { // subclass body } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class employee { var empName: String = "" var empAge: Int = 0 var empSalary: Int = 0 init(empName: String, empAge: Int, empSalary: Int) { self.empName = empName self.empAge = empAge self.empSalary = empSalary } } class manager : employee {} class engineer : employee { } let mng = manager.init(empName: "Keith", empAge: 25, empSalary: 4000) let eng = engineer.init(empName: "John", empAge: 25, empSalary: 2000) print("W3Adda - Swift Inheritance Subclass") print("Manager's Name is :\(mng.empName)") print("Manager's Age is :\(mng.empAge)") print("Manager's Salary is :\(mng.empSalary)") print("Engineer's Name is :\(eng.empName)") print("Engineer's Age is :\(eng.empAge)") print("Engineer's Salary is :\(eng.empSalary)") |
Output:-
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class employee { func sayHello(){ print("Hello from employee.") } } class manager : employee { override func sayHello(){ print("Hello from manager.") } } class engineer : employee { override func sayHello(){ print("Hello from engineer.") } } let emp = employee() let mng = manager() let eng = engineer() emp.sayHello(); mng.sayHello(); eng.sayHello(); |
Output:-
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class employee { var empPos: String = "employee" var pos: String { return "Emploee\'s position is \(empPos)" } } class manager: employee { var mngPos = "manager" override var pos: String { return super.pos + ", the override position is \(mngPos)" } } let emp = manager() emp.empPos = "engineer" emp.mngPos = "Sr. Engineer" print("W3Adda - Swift Overriding Properties") print("\(emp.pos)") |
Output:-
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class employee { var empPos: String = "employee" var pos: String { return "Emploee\'s position is \(empPos)" } } class manager: employee { var mngPos = "manager" override var pos: String { return super.pos + ", the override position is \(mngPos)" } } let emp = manager() emp.empPos = "engineer" emp.mngPos = "Sr. Engineer" print("W3Adda - Swift Overriding Property Observer") print("\(emp.pos)") class techlead: manager { override var empPos: String { didSet { mngPos = empPos } } } let emp1 = techlead() emp1.empPos = "techlead" print("Position is \(emp1.empPos)") |
Output:-
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.