Question: The GenericFruit class declares the following method.
Public void setCalorieContent(float f)
You are writitng a class Apple to extend GenericFruit and wish to add methods which overload the method in GenericFruit.
Select all of the following which would constitute legal declarations of overloading methods.
a. protected float setCalorieContent(
String s)
b. protected void setCalorieContent(float x)
c. public void setCalorieContent(double d)
d. public void setCalorieContent(String s) throws NumberFormatException
The key answer is a,c,d However , I am wondering about
answer c. Look at following code which comes from this forum:
public class work23 {
work23()
{
System.out.println("default");
}
work23(int i)
{
System.out.println("int");
}
work23(long l)
{
System.out.println("long");
}
public void draw(float l)
{
System.out.println("draw parent");
}
public static void main ( String a[])
{
work23 w = new work23(23);
child ch = new child(10);
ch.draw(10.0F);
}
}
class child extends work23 {
child(int k)
{
System.out.println("child int");
}
public void draw(double ll)
{
System.out.println("drawing long child");
}
}
Compile error! The line ��ch.draw(10.0F);�� is supposed to call the method inherited from superclass work23 because the compiler will look up the ��most specific�� method (which is the one inherited from superclass work23). But there is a compiler error?
Can you guys explain something on this? Thank you very much!