This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused in overriding question

 
Mark Guo
Ranch Hand
Posts: 58
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Why the answer is "Parent" rather than "Chat"
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources.

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

The answer to what question ?
 
Mark Guo
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is Why the answer is "Parent" rather than "Chat"???
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bharath Raja
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first clear me your class name which contains main method is "Chat" or "Chat1"...? If two are different, then where is your "Chat" class.
 
Mark Guo
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean the out put is "Parent" not "Chat" why?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still there is a typo on 7th line, and QuoteYourSources
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then if I asked, why you are expecting Chat as the output?
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Guo
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hrishikesh Yeshwant Alshi, thanks for your time and patience.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still didn't quote your sources. Where does this code come from ?
 
Mark Guo
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hrishikesh Yeshwant Alshi the code should be


the parameter should as same as Parent.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, you don't need the @Override annotation here.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic