Online Lab 1
Object Oriented Programming
Learning Objectives
1. The students will be able to
Implement the inheritance relationship between two classes having an Is-A relationship
Override and reuse base class behavior in the derived class
Override the common object class methods such as toString
Lab Walkthrough/Demo
Lab Demo 12.1. Inheritance in action. Reference:
THE Test CLASS
public class TestCylinder { // save as "TestCylinder.java"
public static void main (String[] args) {
// Declare and allocate a new instance of cylinder
// with default color, radius, and height
Cylinder c1 = new Cylinder();
// Declare and allocate a new instance of cylinder
// specifying height, with default color and radius
Cylinder c2 = new Cylinder(10.0);
// Declare and allocate a new instance of cylinder
// specifying radius and height, with default color
Cylinder c3 = new Cylinder(2.0, 10.0);
System.out.println("Cylinder1 data:" + c1
+ " Cylinder2 data=" + c2
+ " Cylinder3 data=" + c3);
}
}
Home Task
The Source Class:
Now complete the following tasks inside the Employee class:
1. Consider a base class named Employee and its derived classes HourlyEmployee and
PermanentEmployee while taking into account the following criteria.
a. Employee class has two data fields i.e. a name (of type string) and specific empID (of
type integer)
b. It has a toString method, which returns a string in the following format.
i. Employee: Ali (ID: 786)
c. Both classes (HourlyEmployee and PermanentEmployee) have an attribute named
hourlyIncome, while the HourlyEmployee has an extra field named totalHours
d. Both classes (HourlyEmployee and PermanentEmployee) have three-argument
constructors to initialize the hourlyIncome as well as data fields of the base class.
i. The id and name should be initialized using the constructor of the employee
class
ii. The totalHours for HourlyEmployee should be input from user right inside the
constructor
e. Class HourlyEmployee has a function named calculate_the_hourly_income to calculate
the income of an employee for the actual number of hours he or she worked.
f. Similarly, PermanentEmployee class has function named calculate_the_income to
calculate the income of an employee that gets paid the salary for exact 180 hours, no
matter how many actual hours he or she worked.
g. Implement the toString Method for both classes. The method should return the string
formatted according to following for each class:
HourlyEmployee PermanentEmployee
Employee: Ali (ID: 786) Employee: Ali (ID: 786)
Printed using This month salary = 150 * 500 = 75000 This month salary @500 PKR/Hour = 90000
toString of
Employee class
The Test Class:
1. Develop the main() method inside the employee class, create an instance of both classes
(i.e. HourlyEmployee and PermanentEmployee) with the data of your own choice.
2. Test the working of functions that calculate the total income of an employee.
3. Test the toString methods of both classes by printing both objects in a print statement.