Q:

Swift program to implement multilevel inheritance

belongs to collection: Swift Inheritance Programs

0

Here, we will create three classes PersonEmployee, and Accountant with data members. Then we will inherit the Person class to Employee class and Employee class into Accountant class to implement multilevel inheritance.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to implement multilevel inheritance is given below. The given program is compiled and executed successfully.

// Swift program to implement multilevel 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 Accountant: Employee {
    var salary: Float = 0
    var bonus: Float = 0
    
    func setInfo(id: Int, name: String, age:Int, s: Float, b: Float) {
        setEmp(id:id, name:name, age:age)
        salary = s
        bonus = b
    }

    func printInfo() {
        printEmp()
        print("\tEmployee Salary: ", salary)
        print("\tEmployee Bonus : ", bonus)
    }
}

var acc1 = Accountant()
var acc2 = Accountant()

acc1.setInfo(id:101, name:"Arun",age:22, s:12345.23, b:220.34)
acc2.setInfo(id:102, name:"Amit",age:25, s:12000.45, b:245.65)

print("Accountant1:")
acc1.printInfo()

print("Accountant2:")
acc2.printInfo()

Output:

Accountant1:
        Employee Id  :  101
        Name:  Arun
        Age :  22
        Employee Salary:  12345.23
        Employee Bonus :  220.34
Accountant2:
        Employee Id  :  102
        Name:  Amit
        Age :  25
        Employee Salary:  12000.45
        Employee Bonus :  245.65

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift

Here, we created three classes PersonEmployee and Accountant.

The Person class contains data member Name and Age. The Employee class contains a data member's "empId.The Accountant class also contains two data members salary and bonus.

All classes contain methods to set and print data members. Then we inherited the Person class into the Employee class and the Employee class into the Accountant class. After that, we created the objects of the Accountant class and set and print Accountant information.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Swift program to implement hierarchical inheritanc... >>
<< Swift program to implement single inheritance...