ADVERTISEMENT
ADVERTISEMENT

Scala program to implement hierarchical inheritance

Here, we are going to learn how to implement hierarchical inheritance in Scala programming language?
Submitted by Nidhi, on June 08, 2021

Problem Solution:

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.

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.

// 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();

  }
}

Output:

Employee Information:
	Employee Id  : 101
	Name: Arun
	Age: 23
	Salary  : 12345.230469
Student Information:
	Student Id: 1001
	Name: Anup
	Age: 14
	Fees  	: 5000.229980

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.

Scala Inheritance Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.