2.What is the purpose of the operator \"instanceof\"? 3.What do you mean by down
ID: 3815433 • Letter: 2
Question
2.What is the purpose of the operator "instanceof"? 3.What do you mean by downcasting? Why do you need it? 4. Which part of program shows polymorphic behavior? Explain... Employee java 6 package csci207; 8 public class PayrollsystemTest 1ee public static void main(String[] args) create subclass objects SE se new SE("John", Smith 111-11-1111 800.00) HE he new HE("Karen", Price 222-22-2222 16.75, 40) CE ce new CE("Sue", Jones 333-33-3333 10000 .06) BPCE bpce new BPCE("Bob", Lewis 444-44-4444 5000, 04, 300) system. out.println("Employees processed individually: system out. printf("xnxsxnxs: $%,.2fxnxn", see earned se earnings()) System.out.printf("%sxnxs: $%,.2f%n%n", he earned he earnings 21 System. out printf ("xsxnxs: $6, .2fxn%n", ce, earned ce earnings system. out. printf("%sxnsks: $x, .2fxntin", bpce, earned bpce earnings()); create four-element Employee array 25 Employee employees new Employee [4] 26 initialize array with Employees employees[0] see employees[1] he: employees[2] ce; employees [3] bpce; system out. printf("Employees processed polymorphically nxn");I generically process each element in array employees 35 for (Employee currentEmployee employees) system out printin (currentEmployee); invokes tostring determine whether element is a BasePluscommission Employee if (currentEmployee instanceof BPCE) downcast Enployee reference to Smart Insert 33 71Explanation / Answer
1. Ouput from SuperClass: Superclass is the topmost class from which all other classes are derived. In Java, this class is the Object class. For this program, the super class is Employee from which all other classes, SE, HE, CE, BPCE are derived.The output from superclass is the name and ID number of the employee which is common among all subclasses and is present in the supercalss.
2. instanceof Operator: instanceof Operator tests whether the specified object is an instance of the class or not.
Syntax: <object> instanceof <class>
It returns TRUE if the object is an instance of the class, otherwise FALSE
3. Downcasting: Downcasting is when an object of the superclass is typecasted to the subclass type. Downcasting is possible when you try cast an object from the superclass which is created as type sub-class.
For example, in the above code,
in line 41, you are checking if current employee is an instance of BPCE employee and only if yes, you are downcasting current employee to BPCE class. Suppose current employee is an instance of Employee class and you try to downcast it, you will run into a run-time exception.
4. The part of the program from line 26 shows polymorphismic behaviour.
Reason: You have created an array of objects of superclass Employee in line 26. But from line 28, the objects in the array are being assigned to sub-class objects of SE,HE, CE, BPCE. This is polymorphism. When a superclass reference is used to refer a sub-class object, the ability to do so is called Polymorphism. It is basically the ability of an object to take any form.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.