Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

overriding and overloading program

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

How i can find the difference in method overriding and overloading with regard to below program. And also how to find valid overload and valid override with regard to the below code.
class Big{
void doStuff(int x){ }
}
class Heavy extends Big{
//tell me whether below statement is valid overload /override
void doStuff(byte b){}
protected void doStuff(int x)throws Exception{}
}
public class Weighty extends Heavy{
void doStuff(int x);
//tell me whether below statement is valid overload /override
String doStuff(int x){return "hi";}
public int doStuff(int x){return 7;}
//tell me whether below statement is valid overload /override
private int doStuff(char c) throws Error{return 1;}
}

i have added comment to the code which explains my doubt. Can anyone explain the reason as well? I took the above example from ocp practice exams by K&B
Thank you campbell for your response.
 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don’t understand the question; you need to explain a lot before we can help you. Please quote the whole of it, and also tell us where it is from. Please edit the code to add code tags and indentation and remove the line numbers because the [code] tags take care of that. I can’t tell how many lines are supposed to be //commented out.

And welcome to the Ranch
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have modified your code such that it compiles and added some comments which should answer your questions.

I suggest you to read the Java documentation to understand the difference between overriding and overlaoding.

In short:
Overloading = same method name but different signature (thus different list of parameters)
Overriding = same method name + same signature

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic