If a method in a parent class is declared as throwing a certain type of exception, is it necessary for the overriding methods in the subclasses to also declare themselves as throwing this exception? Or when they are called polymorphically, will they know to throw this exception as they extend the parents method anyway? thanks in advance
Hi, As far as I know,polymorphism is overloading and NOT method over-riding. For method over-riding 1)arguments lists should be of identical type and order. 2)return type should be same to that of method it over rides 3)the accessibilty cannot be more restricted than the original method. 4)The method must not throw checked exceptions of classes that are not possible for the original method. For method over-loading as such there are no restrictions but I believe just changing the return type (and keeping remaining things unchanged) will create problems. Regards, Amit
thanks very much -I am still a bit confused though as I thought that polymorhism involved overriding by using more specific implementations of certain methods if they are 're-defined' in the child classes? is this wrong? if not do the more specific methods need to declare the Exceptions listed in the methods that they are overriding?
Hi Peter, If a parent class is throwing an expection, the subclass can throw the same exception or the subclass of the same exception.It depends on the requirement of the application whether you would like your subclass method to throw that exception or not. However, if you view this aspect from the UML specification perspective, it means the subclass must conform to the responsibilities of the superclass.Since you have defined that your superclass method throws an exception, you would generally want your subclass method to adhere to that responsibility, i.e. you would define the subclass method to throw atleast the same (or its subclass!) exception. Polymorphism could mean "overriding" or "overloading".Overloading is when you have the same method name with different set of arguments.The above principle is overriding. Let me know if you still have doubts. -- Sandeep Refined my post for Polymorphism. [This message has been edited by Desai Sandeep (edited July 11, 2001).]
thankyou Sandeep - that has sorted out the question of the Exceptions... I thought that polymorphism was when you called a method on an object and depending on what sort of object it is it will behave differently - so that if a parent class Person has a method like cut() which is overridden in its two subclasses Actor and DressMaker, running through an array of Person objects calling cut() the objects in the Person array will behave differently depending on if they are Actor or DressMaker objects... isn't this right or am I completely off the mark here? many thanks...
Originally posted by Peter Guillebaud: [..]I thought that polymorphism was when you called a method on an object and depending on what sort of object it is it will behave differently[..]
There are infact four terms which can be very confusing - "Overloading","Overriding","Polymorphism", and "Dynamic Binding". In general, Polymorphism means "many forms" of the methods.Since "Overloading" and "Overriding" both produce different forms (implementation) of the method, we could say that they exhibit Polymorphism. However, Overloading produces a method with the a different signature - although the method name is same, the arguments(parameters) are different.I would say this is Polymorphism, because the name of the method is still same; this is still one form of the method! In UML, you have a Polymorphism GRASP pattern, which doesn't put the definition in terms of "overloading" and "overriding".It talks about assigning responsibilities to the subclasses and removal of conditional logic from the code.So you may have a method in the superclass, which you may overload/override for assigning proper responsibilities in your subclass.Hence, Polymorphism may mean both overloading and overriding.
Originally posted by Peter Guillebaud: [..]- so that if a parent class Person has a method like cut() which is overridden in its two subclasses Actor and DressMaker, running through an array of Person objects calling cut() the objects in the Person array will behave differently depending on if they are Actor or DressMaker objects... isn't this right or am I completely off the mark here?[..]
You would override the cut() method of the parent class - Person in the two subclasses - Actor and DressMaker, if you define the method (with the same signature) in the subclass. You would overload the cut() method if you define cut(String) or cut(int,int) in your subclasses.Infact, you may overload a method in the same class if you like. Overloading the method in the superclass is not in accordance to Polymorphism GRASP Pattern.So we may infer "overloading" is not "Polymorphism" Personally, I don't like to get into definition war, as long as I understand what is required to design applications. When we talk about, which method would be called at runtime, the principle of "dynanic binding" comes in.This is resolved at runtime.An array of Person objects behaving differently because it holds the instances of different subclasses is an example of "Dynamic Binding" at runtime not "Polymorphism".We can only infer that "Dynamic Binding" comes into picture because of "Polymorphism"; but certainly they are not same. Hope this helps, Sandeep [This message has been edited by Desai Sandeep (edited July 11, 2001).]