Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: Use the Java hierarchy program below add one overriding method and one overloading. The main method should create an instance of the class and

Question: Use the Java hierarchy program below add one overriding method and one overloading. The main method should create an instance of the class and demonstrate the correct functionality of the overriding and overloading methods. Include a screenshot of the execution.

//CREATING CLASS HIERACHY //Superclass: UEmployee //Subclasses: Faculty, Staff

import java.util.*;

class UEmployee { //declaration of name and salary String empName; double salary; //constructor of UEmployee UEmployee(String empName,double salary) { this.empName=empName; this.salary=salary; } //function to return the employee name String getName() { return empName; }

//function to return the salary double getSalary() { return salary; }

public String toString() { String output=""; output=output+"Name: "+getName()+" "; output=output+"Salary: $"+getSalary()+" "; return output; } }

class Faculty extends UEmployee { String deptName; //constructor of the Faculty Faculty(String nm,double sal,String dnm) { //passing the name and salary to superclass constructor super(nm,sal); deptName=dnm; }

//function to return department name String getDeptName() { return deptName; }

public String toString() { System.out.println(super.toString()); return "Departname: "+getDeptName()+" "; } }

class Staff extends UEmployee { String jobTitle;

//constructor of Staff class Staff(String nm,double sal,String title) { //passing the name and salary to superclass constructor super(nm,sal); jobTitle=title; }

//function to return job title String getJobTitle() { return jobTitle; }

public String toString() { System.out.println(super.toString()); return "Job Title: "+getJobTitle()+" "; } }

//test or driver class class Test { public static void main(String args[]) {

//accepting the required values from user using Scanner class String nameF,nameS,dname,title; double salF,salS; Scanner sc=new Scanner(System.in); System.out.print(" Enter the faculty name: "); nameF=sc.nextLine(); System.out.print("Enter the salary: "); salF=sc.nextDouble(); System.out.print("Enter the department name: "); dname=sc.next();

sc.nextLine(); System.out.print(" Enter the staff name: "); nameS=sc.nextLine(); System.out.print("Enter the salary: "); salS=sc.nextDouble(); System.out.print("Enter the Job title: "); title=sc.next();

//Instantiation of Faculty class Faculty fac=new Faculty(nameF,salF,dname); System.out.println(" Details of Faculty:"); System.out.println("----------------------------------"); System.out.println(fac);

//Instantiation of staff class Staff st=new Staff(nameS, salS,title); System.out.println(" Details of Staff:"); System.out.println("----------------------------------"); System.out.println(st); System.out.println(" ");

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions