”;
Inheritance is the most commonly used feature of object-orientated programming, it allows a class (subclass or derived class) can inherit methods, properties and functionalities from another class (base class or superclass). It provides a mechanism to organize and reuse code. It is also used to create a hierarchical relationship between classes. Swift also provides access control modifiers to control the visibility of properties and methods in base and sub-classes.
In Swift, classes can be further categorized into sub-class and super-class −
-
Sub Class − when a class inherits properties, methods and functions from another class it is called a subclass or derived class.
-
Super Class − A class containing properties, methods and functions to inherit other classes from itself is called as a superclass or base class.
Base Class
A class that does not inherit methods, properties or functions from another class is known as the Base Class. Or we can say that the base class is a foundational class for other classes that derive from it. A base class is also known as a superclass. It provides a set of methods, properties, and behaviour that can be shared among its subclasses. A base class is defined just like a regular class, which means using a class keyword.
Syntax
Following is the syntax of the base class −
class BaseClassName{ // Properties // Methods }
Example
Swift program to demonstrate how to create a base class.
// Base class class StudDetails { // Properties var stname: String var mark1: Int var mark2: Int var mark3: Int // Initializer init(stname: String, mark1: Int, mark2: Int, mark3: Int) { self.stname = stname self.mark1 = mark1 self.mark2 = mark2 self.mark3 = mark3 } } // Instance of the base class let stObj = StudDetails(stname: "Swift", mark1: 98, mark2: 89, mark3: 76) // Accessing the Properties print(stObj.stname) print(stObj.mark1) print(stObj.mark2) print(stObj.mark3)
Output
It will produce the following output −
Swift 98 89 76
Subclass
A class is created from the base class or an existing class is known as a subclass. A subclass inherits the methods and properties of the base class and can also have its own properties and methods. It can also override the properties and methods inherited from the superclass. A subclass can also have subclasses, they inherit or override the properties and methods of both the immediate base class(subclass) and base class, and also have their own additional properties and methods.
A subclass is defined by using a class keyword followed by the name of the subclass, a colon and the name of the base class from which it will derive. In a subclass, we have to use super.init in the subclass’s initializer to make sure that the initialization code from the base class will execute before the initialization code of the subclass.
Syntax
Following is the syntax of the subclass −
class SubClassName : BaseClassName{ // Properties // Methods }
Example
Swift program to demonstrate subclass.
// Base Class class StudDetails { // Properties var name: String var age: Int var className: String // Initializer init(name: String, age: Int, className: String) { self.name = name self.age = age self.className = className } // Method func Show() { print("Name: (name), Age: (age), className: (className)") } } // Sub class class Marks: StudDetails{ // Properties var mark1: Int var mark2: Int // Initializer init(mark1: Int, mark2: Int, name: String) { self.mark1 = mark1 self.mark2 = mark2 // This Initializer will execute before the Initializer of sub-class super.init(name: name, age: 18, className: "3rd") } // Overriding method of base class override func Show() { print("Mark1: (mark1) and Mark2: (mark2)") } } // Creating an instance of a subclass let obj = Marks(mark1: 23, mark2: 45, name: "Mohita") // Accessing the method obj.Show() // Accessing the properties of the base class using the subclass instance print("Name:", obj.name) print("Age:", obj.age)
Output
It will produce the following output −
Mark1: 23 and Mark2: 45 Name: Mohita Age: 18
Type of Inheritance
Swift supports the following type of inheritance −
- Single
- Multilevel
- Hierarchical
Swift class does not support multiple inheritance to avoid some complexities and challenges such as diamond problems, increased coupling, and initialization challenges. But we can achieve multiple inheritance using Protocols.
Single Inheritance
A single inheritance is an inheritance where a class is derived from only one superclass and a subclass can access all the properties and methods of the superclass. For example, Class Y is derived from Class X.
Example
Swift program to demonstrate single inheritance.
// Base Class class Employee { // Properties var name: String var age: Int // Initializer init(name: String, age: Int) { self.name = name self.age = age } // Method func Show() { print("Name: (name), Age: (age)") } } // Subclass class EmpDetails: Employee{ // Properties var department: String var salary: Int var joiningYear: Int // Initializer init(department: String, salary: Int, joiningYear: Int) { self.department = department self.salary = salary self.joiningYear = joiningYear // Calling superclass initializer super.init(name: "Rohit", age: 23) } // Method func Display() { print("Department: (department), Salary: (salary), and Joining Year: (joiningYear)") } } // Creating an instance of a subclass let obj = EmpDetails(department: "HR", salary: 23000, joiningYear: 2021) // Accessing the method of superclass obj.Show() // Accessing the methods of the subclass obj.Display()
Output
It will produce the following output −
Name: Rohit, Age: 23 Department: HR, Salary: 23000, and Joining Year: 2021
Multi-level Inheritance
A multi-level inheritance refers to an inheritance where a class is derived from another class and then another class is derived from the subclass. Or we can say that, multi-level inheritance is a chain of more than two-level inheritance. For example, Class Y is derived from Class X and then Class Z is derived from Class Y.
Example
Swift program to demonstrate multi-level inheritance.
// Base Class class Employee { // Properties var name: String var age: Int // Initializer init(name: String, age: Int) { self.name = name self.age = age } // Method func Show() { print("Name: (name), Age: (age)") } } // Sub class class EmpDetails: Employee{ // Properties var department: String var salary: Int // Initializer init(department: String, salary: Int) { self.department = department self.salary = salary // Calling the initializer of Employee class super.init(name: "Rohit", age: 23) } // Method func Display() { print("Department: (department) and Salary: (salary)") } } // Another sub-class derived from EmpDetails class class EmpWork: EmpDetails{ var joiningYear : Int // Initializer init(joiningYear: Int) { self.joiningYear = joiningYear // Calling the initializer of EmpDetails class super.init(department: "HR", salary: 23000) } // Method func DisplayData(){ // Here we are accessing the Properties of Employee and EmpDetails classes print("Employee Name: (name), nDepartment: (department), and Joining Year: (joiningYear)") } } // Creating an instance of EmpWork class let obj = EmpWork(joiningYear: 2021) // Accessing the method obj.DisplayData()
Output
It will produce the following output −
Name: Rohit, Age: 23 Department: HR, Salary: 23000, and Joining Year: 2021
Hybrid Inheritance
A hybrid inheritance refers to an inheritance where multiple classes will derived from a single base class. These classes can access and override the properties and methods of the base class and can have their own properties and methods. For example, Class Y is derived from Class X and then Class Z is derived from Class X.
Example
Swift program to demonstrate hybrid inheritance.
// Base Class class Employee { // Properties var name: String var age: Int // Initializer init(name: String, age: Int) { self.name = name self.age = age } // Method func Show() { print("Name: (name), Age: (age)") } } // Subclass derived from Employee class class EmpDetails: Employee { // Property var salary: Int // Initializer init(salary: Int) { self.salary = salary // Calling the initializer of Employee class super.init(name: "Rohit", age: 23) } // Method func Display() { print("Employee Name: (name) and Salary: (salary)") } } // Subclass derived from Employee class class EmpWork: Employee{ // Property var joiningYear : Int // Initializer init(joiningYear: Int) { self.joiningYear = joiningYear // Calling the initializer of Employee class super.init(name: "Mohina", age: 22) } // Method func Display(){ // Here we are accessing the properties of Employee and EmpWork classes print("Employee Name: (name) and Joining Year: (joiningYear)") } } // Creating an instance of a subclass let obj1 = EmpDetails(salary: 24000) let obj2 = EmpWork(joiningYear: 2022) // Accessing the method of EmpDetails class obj1.Display() // Accessing the method of EmpWork class obj2.Display()
Output
It will produce the following output −
Employee Name: Rohit and Salary: 24000 Employee Name: Mohina and Joining Year: 2022
”;