• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Here comes another one

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this code is from mock exam regarding the overriding and overloading as well, and I got confused too.
The GenericFruit class define a method return a float value calculated from serving size:
public float calories(float servesize);
The Apple class extends the GenericFruit class declares the following overriding method:
public double calories (double amount);
What result would you predict?
1. it won't compile because of the different return type.
2.it won't comile because of the different input type.
3. it will compile but it will not override the GenericFruit method because of the different parameter.
The answer is 1, but I think it should be 3.
any one want to chime in ?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a case of Overloading. Look different arguments,float & double. 3 is the answer.
Not overriding because for overriding name,args,returntype should be same.
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason,
Based on the problem description I have written a small program to illustrate the concept.

class GenericFruit {
public float calories( float servesize) { return servesize;}
}
public class Apple extends GenericFruit {
public double calories (double amount) { return amount;}
}
The method calories defined in GeneraicFruit class is overloaded in the class Apple. This is not an example of overriding since the argument types are differnt (here the name of the argument does not matter). So the correct answer is 3 (program compiles without giving any compliation error).
Regards,
Milind


[This message has been edited by Milind Kulkarni (edited June 15, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the MOCK EXAM again. I got this question two days ago and
the answer was 3.
Enjoy the day, Monty
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic