A derived Java class can override a method in its base class. For example, a means of payment method in a
Person class might return a generic message; e.g., "Person is paid monthly". In the
Employee class, however, we can be more specific; e.g., "Employee is paid semi-monthly by W-2". Accordingly, in the derived class, we would desire to override the base class payment method. Learn how to override a base class method by following these 10 steps.
howPaidmethod that returns a string specifying how a person is paid. The program also defines two properties:
lastName. Notice that the program follows the Javabean specification for defining a property, i.e., a private variable with getter and setter methods.
Employeeclass provides a
howPaidmethod that will override the corresponding method in the base class. The method returns a string that is specific to how an employee is paid. The program also defines one property:
empId. This property will contain the employee ID.
howPaidmethod of the
Personclass will be called on line 5 of the program source. But when you run the program, you will find that the Employee version of
howPaidwill be called.
howPaidmethod was called.