Forums Register Login

overriding and overloading program

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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

It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1119 times.
Similar Threads
exception in main thread
Throws exception in main()
master exam question?
Understanding Exception Hierarchy
Over-riding method which throws exception
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:39:14.