File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Overridden methods... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About Overridden methods..." Watch "About Overridden methods..." New topic
Author

About Overridden methods...

Bharat Roy
Ranch Hand

Joined: Jul 01, 2004
Posts: 156
hello,

Khalid Mughal book quotes at page 233 that "overridden method in the superclass is NOT inherited by the sub class".

I tried the following code:



The output was:

B's f()
C's call()

This means that the overridden method f() was inherited by the class C. Had it not been inherited, java would have never given B's f() as an output. That means overridden method can be inherited.

I'm like . Please Help!!!

Thanks in advance.
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
I have heard that Constructors cannot be inherited but i never heard that Overridden methods cannot be inherited !!!

Infact a method has to inherited to be overridden in the first place...Unfortunately i do not have the Khalid Mughal book....but i guess it must be a typo or sumthing

But since C does not find a method f() defined within it looks up the hierarchy and finds it in B and thus B's f() is called....If B did not have method f()...the it would look further up and find it in A....since C extends B which extends A


---<br />SCJP 1.4
Robert Chisholm
Ranch Hand

Joined: Jul 18, 2004
Posts: 69
Hey, I thought I'd throw this in just to fry your brain some more. Try it




Prints:

B's f()

B's call()


SCJP 1.4<br />(WIP) SCJD B&S v2.3.3
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Robert,this is interesting....

making B's call() method private....gives the output as different to wht it gave before !!!


[ August 11, 2004: Message edited by: Murtuza Akhtari ]
Bharat Roy
Ranch Hand

Joined: Jul 01, 2004
Posts: 156
hi,

sorry, Robert. Instead of replying I accidentally clicked on new post (named "To Robert").

Murtuza, the Khalid book says that during compilation time the compiler sees this method (call() in B class) and assumes that it being private and connot be overridden, it calls B's method at run time .

Personally I am not satisfied with that explation. Nothing was written more than that. First that overridden methods can NOT be inherited and now this. Khalid book is making me nuts
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Originally posted by Ankur Bhatt:
hi,

sorry, Robert. Instead of replying I accidentally clicked on new post (named "To Robert").

Murtuza, the Khalid book says that during compilation time the compiler sees this method (call() in B class) and assumes that it being private and connot be overridden, it calls B's method at run time .

Personally I am not satisfied with that explation. Nothing was written more than that. First that overridden methods can NOT be inherited and now this. Khalid book is making me nuts



Actually tht makes perfect sense IF the call() method to B is private !!!
If a method is private, it can be seen only by the class in which it is defined. NOT even the sub-classes...
In that case, your code...doesnt have a private method in class B...so the output is
B's f()
C's call()
b'coz the object is of type C..and there is no method f() in class C so it goes up to B..but it has a method call()...so it calls C's call()

As to Robert's code...
On calling ob.f() it first goes to the B's f()...now there is an invoking of the method call()
At this point the JVM chks the current class (B)....finds tht the method call() is private....assumes that it cannot be overridden...so calls B's call().
Hence the output
B's f()
B's call()

Before you start cussing Khalid's book
make sure you r reading it properly......Does it say that
Overridden methods cannnot be inherited"
or is it
"Private methods cannot be Inherited and thus cannot be Overridden"

It should be the latter !!!
[ August 12, 2004: Message edited by: Murtuza Akhtari ]
Bharat Roy
Ranch Hand

Joined: Jul 01, 2004
Posts: 156
Hi,

Murtuza,
It doesn't make perfect sense when there is a method in the sub class with the same name i.e. call(). The method has been invoked with the sub class instance only. That method in the sub class was also present at the compile time, then why the JVM thought it that way. It should have checked in the sub class too as the object that has invoked the f()in that of C Class.

Since Khalid wrote that answer, one can easily take it as it is (which is right as they are good at Java). The behavior of JVM surprises us (or me whatever) a lot of time. But still, I am not satisfied with that explanation.....

I guess I clearly wrote the sentence in the Khalid Book, anyway here it is again (for you) the whole sentence:

"The overridden method in the superclass is not inherited by the sub class....." as on page 233 line no 4.

Here's another one for your entertainment pleasure (got to say that as it is amusing).

At page 254 Under sub topic "Extending Interface" (in the context of interface)
Overridden methods are not inherited.

What's that??? If they are not inherited then how come we override it to achieve polymorphism!!!

Private method are private they cannot be inherited and hence no overriding is possible so no point quoting it in the book (it is obvious).

I read each and every book properly. Had I not been reading it properly I would have never pointed out those sentences.

Anyways, you seem to be a big fan of author Khalid(might be some other book), I guess my sentence hurt you. Sorry if that's the case but I still say it is making me sick.... can't help much as that's the only book I am having for the preparation. I wish I had started from K&B book.
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Whoa !!

Chill out Dude!!
I think you took offence to my telling you to read it properly..I was only trying to clarify, since i DONT have the Khalid book(I think i mentioned that before(check my earlier post)But i m mentioning it again(for you) ...I m using the K&B book. Infact i have never read any of Khalid's book. So I m not in a position to give you my views on it. Anyway I would suggest you try to see other books or mebbe ask some others in this forum who really know the stuff !

As per your views on Khalid's book or any book for that matter--Hey they are your views! I m not, and neither will i try to change thm.

Thanks
Bharat Roy
Ranch Hand

Joined: Jul 01, 2004
Posts: 156
Murtuza, I did take your words that way. I never give such comments to someone to whom I am talking/writing too (on his back may be yes ). Anyway, I know you do not have Khalid book (how lucky u r - studying from K&B book I have heard so much good about it.).

Honestly tell me, were you still satisfied with that explanation of Khalid book. I mean it must be that way, but my doubt is also valid.

BTW, how far did you reach in your preparation. When r u planning to give the paper. Actually here, I am on my own (no competitive spirit) so just for that may I know ur email ID (at least then there will be someone for the competition) .

Regards.
 
Consider Paul's rocket mass heater.
 
subject: About Overridden methods...