Swift Program To Implement Hierarchical Inheritance

Swift Program To Implement Hierarchical Inheritance

Swift Program To Implement Hierarchical Inheritance

// Swift program to implement hierarchical inheritance

import Swift

class Person {
    var name: String = ""
    var age: Int = 0

    func setPerson(name: String, age: Int) {
        self.name = name
        self.age = age
    }

    func printPerson() {
        print("\tName: ", name)
        print("\tAge : ", age)
    }
}

class Employee : Person {
    var empId: Int = 0

    func setEmp(id: Int, name: String, age: Int) {
        empId = id
        setPerson(name:name, age:age)
    }

    func printEmp() {
        print("\tEmployee Id  : ", empId)
        printPerson()
    }
}

class Student : Person {
    var stuId: Int = 0
    var fees: Float = 0

    func setStudent(id: Int, name: String, age: Int, fee: Float) {
        stuId = id
        fees = fee
        setPerson(name:name, age:age)
    }

    func printStudent() {
        print("\tStudent Id: ", stuId)
        printPerson()
        print("\tFees: ", fees)
    }
}

var emp = Employee()
var stu = Student()

emp.setEmp(id:101, name:"Arun", age:23)
stu.setStudent(id:1001, name:"Amit", age:15,fee:25000.56)

print("Employee Information:")
emp.printEmp()

print("Student Information:")
stu.printStudent()

Output

Employee Information:
        Employee Id  :  101
        Name:  Arun
        Age :  23
Student Information:
        Student Id:  1001
        Name:  Amit
        Age :  15
        Fees:  25000.56

...Program finished with exit code 0
Press ENTER to exit console.
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment