Before performing such a cast, use the instanceof operator to ensure that the object is indeed an object of an appropriate subclass. We need to be more specific before we can create objects. Recall that the Payable version of the method has a more general name to be applicable to possibly disparate classes. Why would you use an abstract class? So we declare earnings as abstract in superclass Employee because a default implementation does not make sense for that method—there is not enough information to determine what amount earnings should return. In some cases, however, it is useful to declare classes for which the programmer never intends to instantiate objects. A program can create an array of superclass references that refer to objects of many subclass types.

The drawing pro- gram uses Shape variables to manage the objects that are displayed. Class BasePlusCommission- Employee has a constructor lines 9—14 that takes as arguments a first name, a last name, a social security number, a sales amount, a commission rate and a base salary. Note that BasePlusCommissionEmployee’s toString initiates a chain of method calls that span all three levels of the Employee hierarchy. Note that only the methods of class Employee can be called via an Employee variable and Employee, of course, includes the methods of class Object. So, we use these capabilities to determine whether a particular employee object is a Base- PlusCommissionEmployee. Using an interface reference, we can polymorphically invoke any method specified in the interface declaration and in class Object—because a variable of an interface type must refer to an object to call methods, and all objects contain the methods of class Object. A class that is declared final cannot be a superclass i.

We then modify class Employee such that it also implements interface Payable. According to Figure Chapter 10 Object Oriented Programming. Creating and Using Interfaces The class includes a constructor that takes the first name, last name and social security number as arguments lines 11—16 ; get methods that return the first name, last name and social security number lines 25—28, 37—40 and 49—52, respectively ; set methods that set the first name, last name and social security number lines 19—22, 31—34 and 43—46, respectively ; method toString lines 55—59which returns the string representation of Employee ; and abstract method earnings line 62which will be implemented by subclasses.


Chapter 10: Object-Oriented Programming: Polymorphism

Method getPaymentAmount is declared in usong Using an interface reference, we can polymorphically invoke any method declared in the interface, its superinterfaces one interface can extend another and in class Object—a variable of an interface type must refer to an object to call methods, and all objects have the methods of class Object. The interface contains one method, run, which describes the behavior of an object when executed. Although a subclass object also is a superclass object, the two objects are nevertheless different.

10.5 case study payroll system using polymorphism

For example, if class Polyymorphism implements Comparable, its compareTo method could compare Employee objects by their earnings amounts. Polymorpphism a Withdrawal is a type of Transaction, we no longer draw an association line directly between class ATM and class Withdrawal. The next four sections each implement one of the concrete classes.

Objects of any subclasses of the class that implements the interface can also be thought of as objects of the interface type.

Java™ How To Program (Early Objects), Tenth Edition by Harvey Deitel, Paul Deitel

The next example introduces an interface named Payable to describe the functionality of any object that must be capable of being paid and thus must offer a method to determine the proper payment amount due. Click here to sign up. As you can see, obtaining the earnings of the SalariedEmployee, HourlyEmployee and CommissionEmployee polymorphically in lines produces the same result as obtaining these employees’ earnings individually in lines In the Shape hierarchy of Fig.

As we’ll see, each subclass of Employee overrides method toString to create a string representation of an object of that class that contains the employee’s type e.

Chapter Object-Oriented Programming: Polymorphism – CodeProject

When processing objects polymorphically, we typically do not need to worry about the “specifics,” but to adjust the base salary, we do have to determine the specific type of Employee object at execution time. You can declare classes that implement i. Technically, we must include method execute in superclass Transaction so that the ATM or any other class can polymorphically invoke each subclass’s overridden version of this method through a Transaction reference.


Class SalariedEmployee’s constructor polymorphksm the first name, last name and social security number to the Employee constructor line 12 to initialize the private instance variables not inherited from the superclass.

To demonstrate creating and using interfaces, we modify our payroll application to create a general accounts payable application that can calculate payments due for company employees and invoice amounts to be billed for purchased goods.

Note that the Employee constructor does not validate the social security number in this example. We demonstrate abstract classes atudy Section Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape.

It then passes the first name, last name, social security number, sales amount and commission rate to the CommissionEmployee constructor line 12 to initialize the inherited members.

(PDF) Chapter 10 Object Oriented Programming | Yucheng Liao –

For example, a programmer can write a method with a parameter of an abstract superclass type. If that class contains the proper method decla- ration or inherits onethe call is compiled.

Each event-listener interface specifies one or more methods that must be implemented to respond to user interactions. Each subclass will inherit this method, enabling the subclass to access its accountNumber as needed to execute a transaction. Good Programming Practice Part 2, you’ll learn how to build Java GUIs and how to build event handlers to respond to user interactions.

10.5 case study payroll system using polymorphism

Why would you use an abstract class? The output illustrates that the appropriate meth- ods for each class are indeed invoked.

A super- class reference can be used to invoke only the methods declared in the superclass— attempting to invoke subclass-only methods through a superclass reference results in com- pilation errors.