First question: I thought it would output zac but it gives compile time error cause of method lilly() that isnt overriding lilly() in Base properly. Isnt lilly() suppopsed to be considered a new method instead of overriding the one in the Base class?
second question(not related to this though): Whats the difference between a local and an anonymous class?
third question: In the real exam do they tell you the NUMBER of correct answers to choose for a question which has mulitiple answers?
Got some more things to ask but I guess this is enough for now
In the base class, the return type of lilly is void. In the derived class, the return type of lilly is int. You can't do that.
Local classes can be given reference names, where an anonymous inner class isn't given a reference name.
posted 14 years ago
Thanks Keith for your reply.
Though i realize the difference in the return types of the 2 methods but doesnt overriding mean that name, return type, argument type and number of parameters passed should be same in both the methods and in this case the return type is different, so that would mean lilly() in the child class is a brand new method. Am I correct?
I thought it would output zac but it gives compile time error cause of method lilly() that isnt overriding lilly() in Base properly. Isnt lilly() suppopsed to be considered a new method instead of overriding the one in the Base class?
A method signature is not determined by the return type. Thus, the compiler thinks you are trying to override this method (since the rest of the method signature is identical to the one in the base class). In order to override it, the method signature must be identical (i.e. return void, not int). You could alternatively, change the method signature in the derived class by adding some input parameters (i.e. overloading the method) and it would distinguish the methods properly.
Concerning the third question, I've heared that the exam says the exact number of correct questions.
Concerning the first question, just in addition to all other answers please take note for some key rules about overridding and overloading methods:
Rules for overriding methods
1. The argument list must exactly match that of the overridden method. 2.The access level CAN'T be more restrictive than the overridden method. 3.The access level CAN be less restrictive than that of the overridden method. 4.The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception. 5.The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method.
Rules for overloading methods 1.Overloaded methods MUST change the argument list. 2.Overloaded methods CAN change the return type. 3.Overloaded methods CAN change the access modifier. 4.Overloaded methods CAN declare new or broader checked exceptions. 5.A method can be overloaded in the same class or in a subclass.
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself