1. When a method overloads a method from its superclass I understand that the return type may be same or different. What is important is that the number and type of parameters be different. IS THE ORDER OF PARAMETERS SIGNIFICANT AS WELL ? ex:PARENT METHOD public int modifyIt(int a, long b) SUBCLASS METHOD a.public long modifyIt(int a, long b) b. public long modifyIt(long b, int a) ------------------ are either a or b valid ??, are these either overloading or overriding? 2. An overriding method of subclass can only throw THE CHECKED EXCEPTIONS (OR THEIR SUBCLSSES) thrown by the parent class method. what about if this overriding method throws an unchecked exception, like ArithmeticException? is that allowed ??
Kishan Kumar
Ranch Hand
Joined: Sep 26, 2000
Posts: 129
posted
0
Sarimraza, The answer to your first question is , that is a case of overloading. The position of the parameters also affect the method signature and if the positions of the same datatypes are different the method is valid and it is a case of overloading. 2. You can very well throw unchecked exceptions in the subclass or in any class for that matter. You need not throw unchecked exceptions at all, but it is not a error if you wish to do so. Hope this helps, A small request , I think you can very well code and look what is the result and it will really help you in remembering the concepts rather than just taking the tips through theory.
------------------ Regards, V. Kishan Kumar
Regards,<BR>V. Kishan Kumar
madhumathi k
Greenhorn
Joined: Nov 01, 2000
Posts: 5
posted
0
In your example b will pass but a will throw an error saying "The method declared in class sub cannot override the method of the same signature declared in class parent." The method in parent and (a) method in sub class are overridden methods but for the above error. Replace long with int in the sub for (a) solves the problem. Just remember the rules of overloading and overriding : overloading - The number and type of parameters should be different. Overloading is related in the same class. overloading of methods is providing alternate implementation for the overloaded methods. overriding - The number and type of parameters and also the return type should be same. overriding is applicable in parent-sub class. overriding feature totally replaces the implementation of the parent class. An overriding method of subclass can only throw THE CHECKED EXCEPTIONS (OR THEIR SUBCLASSES) thrown by the parent class method. But the overriding methods can throws unchecked exception. Hope this helps!
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Sarim, Your basic premise is right: the method signature depends on both the type and order of the arguments; however, in the example you used you could run into a compile error if the compiler cannot tell if a value is an int or a long. For example:
Where exceptions are involved, an overriding method cannot throw more checked exceptions than those originally declared however they can throw fewer exceptions. Example:
Hope that helps. ------------------ Jane The cure for boredom is curiosity. There is no cure for curiousity. -- Dorothy Parker