Exercise “InheritanceDemo”

Requirements:

  • Create a project called “InheritanceDemo” and this will be the name of the class containing the main method.
  • Create a class called “Employee” with the following private data members:
  • A member called ”name” of type String.
  • A member called “hireYear” of type int.
  • Class “Employee” has the following public methods:
  • A constructor that has parameter the name of the member and the hire year.
  • Interface methods for getting and setting the private members.
  • A method “toString()” that returns a formatted string with the values of the data members.
  • Create a class called “HourlyEmployee” that inherits class Employee and has the following additional private data members:
  • A member called ”wageRate” of type double (denotes the paid amount per hour).
  • A member called “hours” of type double (denotes the amount of worked hours).
  • Class “HourlyEmployee” has the following additional public methods:
  • A constructor that has parameter the name of the member, the hire year, the wage rate, and the amount of hours worked. This constructor should invoke the constructor of the superclass Employee to initialize the name and hire year.
  • Interface methods for getting and setting the additional private members.
  • A method “double getPay()” that returns the amount equal to wageRate*hours.
  • A method “toString()” that returns a formatted string with the values of all data members, i.e., including the inherited name and hireYear.
  • Test classes “Employee” and “HourlyEmployee” by doing the following in the main method of class “InheritanceDemo”:
  • Create one object called johnof class “Employee” with name “John” and hireYear2005.
  • Print the information of object john by calling System.out.println(john), which will invoke the toString() method of class Employee.
  • Create one object called joe of class “HourlyEmployee” with name “Joe”, hireYear 2004, wage rate 50.50, and amount of hours worked 160.
  • Print the information of object joe by calling System.out.println(joe), which will invoke the toString() method of class HourlyEmployee.
  • Print the amount computed by calling joe.getPay().
  • Change the wage rate of object joe to 65.
  • Print the amount computed by calling joe.getPay().
  • Check the messages that you see on the screen. They should be similar to the following list of messages:

Name:John Hire Year:2005

Name:Joe Hire Year:2004 Wage Rate:50.5 Hours:160.0

8080.0

10400.0