aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused in overriding question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused in overriding question" Watch "confused in overriding question" New topic
Author

confused in overriding question

Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58



Why the answer is "Parent" rather than "Chat"
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Please QuoteYourSources.

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

The answer to what question ?


[My Blog]
All roads lead to JavaRanch
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
My question is Why the answer is "Parent" rather than "Chat"???
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jan 21, 2009
Posts: 111
first clear me your class name which contains main method is "Chat" or "Chat1"...? If two are different, then where is your "Chat" class.

Life is either daring something or nothing - Helen Keller
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
I mean the out put is "Parent" not "Chat" why?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Still there is a typo on 7th line, and QuoteYourSources


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Then if I asked, why you are expecting Chat as the output?
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

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.

Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
OCPJP6/SCJP6 86%
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
Thanks Hrishikesh Yeshwant Alshi, thanks for your time and patience.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You still didn't quote your sources. Where does this code come from ?
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
Hi Hrishikesh Yeshwant Alshi the code should be


the parameter should as same as Parent.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

And, you don't need the @Override annotation here.
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

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

Joined: Dec 06, 2010
Posts: 62

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

Joined: Sep 23, 2010
Posts: 422
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.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused in overriding question