Forums Register Login

confused in overriding question

1
+Pie Number of slices to send: Send



Why the answer is "Parent" rather than "Chat"
+Pie Number of slices to send: Send
Please QuoteYourSources.

Why the answer is "Parent" rather than "Chat"


The answer to what question ?
+Pie Number of slices to send: Send
My question is Why the answer is "Parent" rather than "Chat"???
+Pie Number of slices to send: Send
The answer to what question ? You are copy/pasting some code, and telling what the answer is, but there's no question.

And you still didn't tell us where this question came from.
+Pie Number of slices to send: Send
first clear me your class name which contains main method is "Chat" or "Chat1"...? If two are different, then where is your "Chat" class.
+Pie Number of slices to send: Send
I mean the out put is "Parent" not "Chat" why?
+Pie Number of slices to send: Send
Still there is a typo on 7th line, and QuoteYourSources
+Pie Number of slices to send: Send
Then if I asked, why you are expecting Chat as the output?
+Pie Number of slices to send: Send
This is overloading in disguise of overriding. (In case if one is not clear with the rules of overriding). I mean, the method show() in the Chat class is NOT overridden. It is overloaded.

Rule is: An overriding method must have argument lists of IDENTICAL type and order.

Let us elaborate,

1. order is out of question here as there is only one argument.
2. type must be IDENTICAL, this means IDENTICAL. "IS A" is not permitted.

Try compiling the below code which has just one change. I have added @Override annotation to the Chat class's show method. This annotation ensures that the method is ACTUALLY overridden. This means, suppose if developer intended to actually override the method from the super class but mistakenly he missed something and ended up in overloading it then his friend "@Override" annotation will help him by finding his slip EARLY (at compile time).

It is a good practice to use annotation in your code. They are your friends.



By adding the annotation @Override to this code it will not even compile and it will tell the developer that the method is NOT correctly overridden as it violated the above stated rule.
+Pie Number of slices to send: Send
Thanks Hrishikesh Yeshwant Alshi, thanks for your time and patience.
+Pie Number of slices to send: Send
You still didn't quote your sources. Where does this code come from ?
+Pie Number of slices to send: Send
Hi Hrishikesh Yeshwant Alshi the code should be


the parameter should as same as Parent.
+Pie Number of slices to send: Send
And, you don't need the @Override annotation here.
+Pie Number of slices to send: Send
 

Hi Hrishikesh Yeshwant Alshi the code should be



Yes you are right. The code I have posted won't compile because the method in the show() method of Chat class is not correctly overridden.


And, you don't need the @Override annotation here.



We still need it. It's always recommended to have annotations in your code. Imagine during maintenance someone trying to change the show() method in Chat class. Our friend "@Override" will be there to report the mistakes (if any).

ALWAYS have "@Override" annotation in your code when you are intending to override any method.
+Pie Number of slices to send: Send
 

Hi Hrishikesh Yeshwant Alshi the code should be



Yes you are right. The code I have posted won't compile because the method in the show() method of Chat class is not correctly overridden.


And, you don't need the @Override annotation here.



We still need it. It's always recommended to have annotations in your code. Imagine during maintenance someone trying to change the show() method in Chat class. Our friend "@Override" will be there to report the mistakes (if any).

ALWAYS have "@Override" annotation in your code when you are intending to override any method.
+Pie Number of slices to send: Send
Now the output is, Parent in chat class. What I have given is method overriding. So, it uses object type.
Your program is an example of Overloading. It uses reference type.
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1371 times.
Similar Threads
override static method
Problem -- Virtual Method Invocation
Static binding Vs Dynamic Binding
Inheritance
set instance with child
More...

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