Docsity

Understanding Polymorphism & Abstract Classes in Java Employee Inheritance - Prof. Joseph , Study notes of Computer Science

Partial preview of the text

Download Understanding Polymorphism & Abstract Classes in Java Employee Inheritance - Prof. Joseph and more Study notes Computer Science in PDF only on Docsity!

Polymorphism  (^) Enables you to “program in the general” rather than “program in the specific.”  (^) Polymorphism enables you to write programs that process objects that share the same superclass as if they’re all objects of the superclass; this can simplify programming.

Classes that can be used to instantiate objects are called concrete classes.  Such classes provide implementations of every method they declare (some of the implementations can be inherited).  Abstract superclasses are too general to create real objects—they specify only what is common among subclasses.  Concrete classes provide the specifics that make it reasonable to instantiate objects.  Not all hierarchies contain abstract classes.

Programmers often write client code that uses only abstract superclass types to reduce client code’s dependencies on a range of subclass types.  (^) You can write a method with a parameter of an abstract superclass type.  (^) When called, such a method can receive an object of any concrete class that directly or indirectly extends the superclass specified as the parameter’s type.  Abstract classes sometimes constitute several levels of a hierarchy.

Cannot instantiate objects of abstract superclasses, but you can use abstract superclasses to declare variables  (^) These can hold references to objects of any concrete class derived from those abstract superclasses.  (^) Programs typically use such variables to manipulate subclass objects polymorphically.  Can use abstract superclass names to invoke static methods declared in those abstract superclasses.

Use an abstract method and polymorphism to perform payroll calculations based on the type of inheritance hierarchy headed by an employee.  Enhanced employee inheritance hierarchy requirements:  (^) A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly employees are paid by the hour and receive overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked in excess of 40 hours, commission employees are paid a percentage of their sales and base- salaried commission employees receive a base salary plus a percentage of their sales. For the current pay period, the company has decided to reward salaried-commission employees by adding 10% to their base salaries. The company wants to write a Java application that performs its payroll calculations polymorphically.

Abstract superclass Employee declares the “interface” to the hierarchy—that is, the set of methods that a program can invoke on all Employee objects.  Each employee has a first name, a last name and a social security number defined in abstract superclass Employee.

The diagram in Fig. 10.3 shows each of the five classes in the hierarchy down the left side and methods earnings and toString across the top.  For each class, the diagram shows the desired results of each method.  Declaring the earnings method abstract indicates that each concrete subclass must provide an appropriate earnings implementation and that a program will be able to use superclass Employee variables to invoke method earnings polymorphically for any type of Employee.

All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  (^) Known as dynamic binding or late binding.  (^) Java decides which class’s toString method to call at execution time rather than at compile time  A superclass reference can be used to invoke only methods of the superclass—the subclass method implementations are invoked polymorphically.  Attempting to invoke a subclass-only method directly on a superclass reference is a compilation error.

A final method in a superclass cannot be overridden in a subclass.  (^) Methods that are declared private are implicitly final, because it’s not possible to override them in a subclass.  (^) Methods that are declared static are implicitly final.  (^) A final method’s declaration can never change, so all subclasses use the same method implementation, and calls to final methods are resolved at compile time—this is known as static binding.