Q:

Scala program to implement multilevel inheritance

belongs to collection: Scala Inheritance Programs

0

Here, we will create three classes to implement multilevel inheritance. The extends keyword is used to implement inheritance. In the multilevel inheritance base class is inherited into derived class. Then derived class is also inherited into another derived class.

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 on the ubuntu 18.04 operating system successfully.

// Scala program to implement multilevel inheritance

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

  def setPerson(name: String, age: Int) {
    this.name = name;
    this.age = age;
  }
  def printPerson() {
    printf("\tName: %s\n", name);
    printf("\tAge: %d\n", age);
  }
}

class Employee extends Person {
  var empId: Int = 0;

  def setEmp(id: Int, name: String, age: Int) {
    empId = id;
    setPerson(name, age);
  }
  def printEmp() {
    printf("\tEmployee Id  : %d\n", empId);
    printPerson();
  }
}

class Accountant extends Employee {
  var salary: Float = 0
  var bonus: Float = 0

  def setInfo(id: Int, name: String, age: Int, s: Float, b: Float) {
    setEmp(id, name, age);
    salary = s;
    bonus = b;
  }
  def printInfo() {
    printEmp();
    printf("\tEmployee Salary: %f\n", salary);
    printf("\tEmployee Bonus : %f\n", bonus);
  }
}

object Sample {
  def main(args: Array[String]) {
    var acc1 = new Accountant();
    var acc2 = new Accountant();

    acc1.setInfo(101, "Arun", 23, 12345.23f, 220.34f);
    acc2.setInfo(102, "Anup", 24, 22345.23f, 120.34f);

    println("Accountant1:");
    acc1.printInfo();

    println("Accountant2:");
    acc2.printInfo();
  }
}

Output:

Accountant1:
	Employee Id  : 101
	Name: Arun
	Age: 23
	Employee Salary: 12345.230469
	Employee Bonus : 220.339996
Accountant2:
	Employee Id  : 102
	Name: Anup
	Age: 24
	Employee Salary: 22345.230469
	Employee Bonus : 120.339996

Explanation:

In the above program, we used an object-oriented approach to create the program. Here, we created an object Sample.

And, we created three classes PersonEmployee and Accountant. The Person class contains personal information like name and age. And, we used setPerson()getPerson() methods. Employee class contains setEmp() and printEmp() function and Accountant class contains setInfo() and printInfo().

Here, we inherited the Person class into the Employee class, Then the Employee class inherited into the Accountant class using the extend keyword.

Then we defined the main() function in the Sample object. The main() function is the entry point for the program.

In the main() function, we created two objects of the Accountant class and called methods to set and print information regarding Accountant.

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

total answers (1)

Scala program to implement hierarchical inheritanc... >>
<< Scala program to implement simple inheritance...