Alan Ong wrote:... A superclass reference can be used to invoke only methods of the superclass... (← you already had the answer!)
At line 49, it uses the instanceof operator to determine if the Employee object's type is BasePlusCommissionEmployee and if it is true that the object referenced by currentEmployee is a BasePlusCommissionEmployee then we shall have to increase their base salary by 10%. Line 53-54 shall then downcast the object type to BasePlusCommissionEmployee and invoke the busclass' getBaseSalary and setBaseSalary on the current Employee object.
What I don't understand is that since through the array of objects we could invoke the relevant object type's toString method why can't we do the same as in to invoke the methods, getBaseSalary and setBaseSalary directly and have to downcast before doing so.
The loop variable, currentEmployee, is of type Employee, the superclass of all other XXXEmployee objects. On lines 56-60, you are accessing methods that only the BasePlusCommissionEmployee class has. Per your understanding that I highlighted in the quote, a superclass reference (Employee currentEmployee) can only be used to invoke methods in the superclass. To invoke methods introduced in a subclass, you'd need a reference to that subclass (BasePlusCommissionEmployee employee), hence the need to cast currentEmployee to that type.
Alan Ong wrote:At line 64 why is it that currentEmployee is able to access all the relevant subclasses' earnings method without downcasting the other object types?
Paul Clapham wrote:
Alan Ong wrote:At line 64 why is it that currentEmployee is able to access all the relevant subclasses' earnings method without downcasting the other object types?
Well, as you already know the currentEmployee variable is of type Employee. And Employee has a public method named earnings(). So naturally the Employee variable currentEmployee can call Employee's public method earnings().
Unfortunately I don't think they do. It is a long time since I used that example and I have had the good fortune to learn from people like Jim Yingst, Ulf Dittmer, Fred Rosenberger, Jeanne Boyarsky, Winston Gutkowski, Liutautas Vilda and Junilu Lacar and many others since then. So I have learnt to get suspicious of instanceof. I also had the good fortune to have Elizabeth Norval lecture me. She told me off when I tried to use instanceof in an application with a slight similarity to that discussed here only with shapes rather than text colours. She also laughed like a drain when I misled Joseph Onibokun into writing −cos instead of cos and his clock application resolutely went anti‑clockwise (that was almost the first thread I participated in on what was then called JavaRanch). Elizabeth told me to use polymorphism instead and after about ten minutes I worked out how to do it polymorphically.Junilu Lacar wrote:. . . I suppose Deitel gave this example to illustrate the concepts we just discussed but you should note that this is not a proper way to code/design real-world applications. . . .
Campbell Ritchie wrote:I shall let others think about that. I can't think of any better suggestions at the present.
What about this, where the ints represent percentages?Obviously my increasePay method will have to accept negative arguments not less than −100Junilu Lacar wrote:. . .
Junilu Lacar wrote:. . . bosses seldom trust employees . . .
Junilu Lacar wrote:
There are three kinds of actuaries: those who can count, and those who can't.
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|