20: Swift. Classes and inheritance

In real world we have our own habits and behaviours and we inherit some characteristics from our parents. Inheritance is a real world concept which is introduced in programming. In Swift you can create a Parent class ‘A’ with some behaviours and attributes (methods and variables) and you can create another Child class ‘B’ which can inherit characteristics from Parent class but it can have its own features as well.

In this class hierarchy, class ‘A’ is called parent class (super or base are also known words for parent class) and class ‘B’ is called child or subclass class. Child class inherits all features from its super class and it can have its own features.

Lets create a class called ‘Person’ which has two properties name, age and one behaviour getInfo function.

class Person {
    
    // properties
    var name : String
    var age  : Int
    
    // method
    func getInfo() -> String{
        
        return ("My name is \(name) my age is \(age)")
    }
    
    init(n :String, a: Int){
        name = n
        age = a
    }
}

If some one ask you to create a ‘Student’ class with name, age and studentid. You must do some brainstorming about Student characteristics. See what is common in Person class which you can use in Student class.

Every person has a name, every student has a name. Every person has some age, every student has some age. If you already have a person class  which has all features which a student class requires, you can use inheritance. Inheritance will help you to keep everything clean, manageable without code duplication. You can add new features in Student class which Person class might not have.

What are those features which are not in Person class but are required in Student class. As you have noticed that every student is a person, so all features of a person are important for student but it is not necessary that every person is a student. A person can be a doctor, engineer but it is not necessary that every person will be a student. So the new features which are missing in person class can be added in Student class.

Create a Student class, add one attribute student id and inherit it with Person. Swift provides following syntax for inheritance.

class ChildClass : ParentClass

class Student : Person{
    
    var studentID : Int
    
    init(id : Int){
        
      studentID = id
      
    }
}

You can see that Student class is derived from its super Person class and now Student class contains all features which Person class has. But question is: how to access variables of super class. How to assign a name and age values to parent class.

Super keyword.

super keyword is used to access the methods of super or parent class. To assign the values to super class variables call super.init method. Add more variables in init method of Student class and pass these variables to the super.init method.

class Student : Person{
    
    var studentID : Int
    
    init(id : Int, studentname : String, studentage : Int){
        
        studentID = id
        super.init(name: studentname, age: studentage)
    }
}

Student init() method has three variables, id, studentname and studentage. id is assigned to studentid, studentname and studentage is passed to super.init() method. super.init method will call Person’s init method and it will assign name and age values to parent class. You can create the Student object with variables.

var student1 = Student(id: 123, studentname: "Khan",studentage: 25)

Override method

Notice that you have one method in super class called getInfo. This method returns a string with name and age information. In your child class you need a new version of getInfo method which can have string from super class and can add new string with student id. In Swift you can override your super class method with new method by adding keyword override.

class Student : Person{
    
    var studentID : Int
    
    init(id : Int, studentname : String, studentage : Int){
        
        studentID = id
        super.init(name: studentname, age: studentage)
    }
    
    override func getInfo() -> String {

        let supermessage = super.getInfo()
        return (" \(supermessage) and my ID is \(studentID)")
    }
}

You can see that getInfo method is overridden with new method and getInfo method from parent class is called with super keyword. super.getInfo will return a String from parent class, you can concat it with new string in Student class. Create an object by passing three values

var student1 = Student(id: 123, studentname: "Khan",studentage: 25)
student1.getInfo()

and your output is ” My name is Khan, my age is 25 and my ID is 123″

 

Continue with class and property observers…

 

Download file from GitHub