• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confused in overriding question

 
Ranch Hand
Posts: 58
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Why the answer is "Parent" rather than "Chat"
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is Why the answer is "Parent" rather than "Chat"???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean the out put is "Parent" not "Chat" why?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still there is a typo on 7th line, and QuoteYourSources
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then if I asked, why you are expecting Chat as the output?
 
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hrishikesh Yeshwant Alshi, thanks for your time and patience.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still didn't quote your sources. Where does this code come from ?
 
Mark Guo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, you don't need the @Override annotation here.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic