my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can private method be overriden/overloaded??? 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 "can private method be overriden/overloaded???" Watch "can private method be overriden/overloaded???" New topic

can private method be overriden/overloaded???

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
jim, thank you for your answer to my previous question.
i'm confused here as i read " the private method can be overridden by a private, friendly, protected or public method. sb told me in the other site saying "private methods can't be overridden or overloaded since they can not be inherited. who is right here?
thank you
Thandapani Saravanan
Ranch Hand

Joined: Oct 17, 1999
Posts: 117
I hope i can share my views. A private method is never accessible to the subclass and so, it can not be overridden (if it is instance method) or hidden (if it is a static method).
But overloading is possible within the class.

paul wheaton

Joined: Dec 14, 1998
Posts: 20971

Check out this code sample

Suppose I had this line of code:
CanadianBovine gertrude = new CanadianBovine();
Because you can't do polymorphism with private stuff, you see "mooo".
If I take out "private":

And try my code fragment again, you see "mooo, eh?"
Houston, we have polymorphism.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
why this program runs without error? Isn't the private void moo () method in the canadianBovine class overriding the one in the super class? i think this is the point i got confused: does that mean that the sub class can have its own private method with the same name as the private method in the super class, however it may not override the super's private method ?
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
Yes it can have the same name, no it will not override. A private method does not exist outside the class for all intents an purposes. So a sub class can do anything with the signature of the superclass' private method including having one with a different return type. It would be a brand new method unrelated to the 'super' method.
[This message has been edited by Tony Alicea (edited February 09, 2000).]

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Paul - "public class CanadianBovine inherits Bovine"? Is that the Colorado dialect of Java? Down here we say "extends". We also don't try to put two public classes in the same file, as they each want the file named after them. (:
Diane - technically it's not an override if the base method is private - the child method is considered a separate method with the same name. It looks like an override, I know, but it isn't, because of the way override is defined. It's not that the compiler forbids you to override a private method, rather it's that the compiler will completely ignore the parent private method while it's looking in the child class, so there's nothing you could write in the child class that it would ever consider an "override".

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: can private method be overriden/overloaded???
It's not a secret anymore!