Originally posted by Suma Narayan:
This is the case of method Overloading. Here, method draw(int j) defined in superclass is being overloaded with method draw(float fl) in the subclass child.
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Originally posted by Suma Narayan:
Hi Jyothi,
This is the case of [b]method Overloading. Here, method draw(int j) defined in superclass is being overloaded with method draw(float fl) in the subclass child. At the compile time, the compiler looks at two things and then decides which method is the most appropriate.
1. Determines Method Name.
The first step in processing a method invocation at compile time is to figure out the name of the method to be invoked. Here, the superclass is also searched for all method declarations applicable to this method invocation.
So, in the above mentioned example, since child2 inherits method draw(int j) from the superclass work23, this method is also considered.
NOTE:
If there were any more methods in superclass (e.g. draw(long l, short s), it also would have been considered at this point.
2. Determine Method Signature.
This step uses the name of the method and the types of the argument expressions to locate method declarations that are both applicable and accessible, that is, declarations that can be correctly invoked on the given arguments. There may be more than one such method declaration, in which case the most specific one is chosen. So, here method draw(long l, short s) will be dropped and we will be left with only two applicable methods draw(int j) and draw(float fl).
Now, how to find most appropriate? Here comes the ambiguity.
Find Methods that are Applicable and Accessible
A method declaration is applicable to a method invocation if and only if both of the following are true:
The number of parameters in the method declaration equals the number of argument expressions in the method invocation.
This is already considered.
The type of each actual argument can be converted by method invocation conversion to the type of the corresponding parameter.
When you say, ch.draw(10), the value passed is 10. 10 is a valid argument for both draw(int j) and draw(float f).
So both method satisfy all required conditions. Now, compiler is left with two methods draw(int j) and draw(float f). So, here the compiler is confused and cannot decide which method is more appropriate. You can solve this situation by calling ch.draw(10f).
There are other ways too, but then there is no question of method overloading here.
1. declaring draw(int j) as private in work23. This method will not be accessible to child class.
2. declare draw(int j) without any modifier and define child2 in another package. Here child2 should be in another package or else it will have access to parent method draw(int j). Remember, if you declare a method without any access attribute, it will be inherited(to be more precise accessible) by the derived class if and only if derived class is in the same package as the base class.
I hope this clears your doubt.
For detailed explanation refer JLS:
http://java.sun.com/docs/books/jls/html/15.doc.html#20448
Suma.
[This message has been edited by Suma Narayan (edited May 23, 2000).][/B]
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Infact the draw method in the child class is just an instance method. It is not an overloaded method because there are no methods defined with the same name in child class. It is not overriding either because the signatures don't match.
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|