Here, we will create three classes to implement hierarchical inheritance. The extends keyword is used to implement inheritance. In the hierarchical inheritance, we inherited the base class into two or more than two derived classes.
The source code to implement hierarchical inheritance is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
// Scala program to implement hierarchical 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;
var salary: Float = 0;
def setEmp(id: Int, name: String, age: Int, sal: Float) {
empId = id;
salary = sal;
setPerson(name, age);
}
def printEmp() {
printf("\tEmployee Id : %d\n", empId);
printPerson();
printf("\tSalary : %f\n", salary);
}
}
class Student extends Person {
var stuId: Int = 0;
var fees: Float = 0;
def setStudent(id: Int, name: String, age: Int, fee: Float) {
stuId = id;
fees = fee;
setPerson(name, age);
}
def printStudent() {
printf("\tStudent Id: %d\n", stuId);
printPerson();
printf("\tFees : %f\n", fees);
}
}
object Sample {
def main(args: Array[String]) {
var emp = new Employee();
var stu = new Student();
emp.setEmp(101, "Arun", 23, 12345.23f);
stu.setStudent(1001, "Anup", 14, 5000.23f);
println("Employee Information:");
emp.printEmp();
println("Student Information:");
stu.printStudent();
}
}
Program/Source Code:
The source code to implement hierarchical inheritance is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
Output:
Explanation:
In the above program, we used an object-oriented approach to create the program. Here, we created an object Sample.
Here, we created three classes Person, Employee and Student. And, we derived the Person class into the Employee and Student class.
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 the objects of the Employee and Student class and then set and printed information on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer