• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

another question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
answer to ur second question...
when u r creating ur subclass just remember that it inherits the parent class method also.. in our case, draw method is also overloaded. now when u call ch.draw(10.0F) it is ambiguous for compiler to choose the right method as the default value for float is a double.. try changing the type of the parent class method draw to double..u will get a clean compile
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there...
this looks a slightly tricky question.. had it been ch.draw(10.0) it works fine...bcoz its ch.draw(10.0F)..F is float..and it checks in both the super and child classes..thats why the error..
 
Leon Peeters
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, rvamsi
In the case of above code, we have two methods in Child class(Please correct me if I��m wrong):
Method 1: public void draw(float l) {/*��*/} (Inheriting from superclass work23)
Method 2: public void draw(double ll) {/*��*/} (overloading in Child class)
On the method call ch.draw(10.0F), compiler will decide which is the ��most specific�� method to invoke( That would be Method 1). Am I right?
So it won��t be ambiguous in fact. How the compiler complain about ambiguous method? I am confusing about this.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi leon,
You are calling ch.draw(10.0F). ch is the instance of child..so when u say ch.draw(10.0F) it will first look in child class. Then it will go upstream and look for the parent class(for the overloading etc)..there it found another similar method..
method1 : draw(double ll)
method2 : draw(float l)
when we say ch.draw(10.0F).this is a float arg.so method1 will work fine(as double can handle float and double is also the default).. but the parent class also similar method(i.e.method2) which can handle the ch.draw(10.0F)..so thats the reason why it is not able to decide.. hope this helps...
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Sorry , I was busy in preparing my exam.
Look at following code with a little difference :
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(double ll)
{
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(float l)
{
System.out.println("drawing long child");
}
}
compile successfully!
We still have two methods in Child class. But this time is ok?
And the compiler don't look upstream to superclass this time?
Would anyone explain this clearly?

[This message has been edited by Leon Li (edited July 10, 2000).]
 
Grow your own food... or this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic