• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding and overloading program  RSS feed

 
crazy vi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • 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
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!