Meaningless Drivel is fun!*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can private method be overriden/overloaded???" Watch "can private method be overriden/overloaded???" New topic
Author

can private method be overriden/overloaded???

Anonymous
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.

Saravanan
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20492
    ∞

Check out this code sample


Suppose I had this line of code:
CanadianBovine gertrude = new CanadianBovine();
gertrude.crackWhip();
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
Anonymous
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
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
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
Wanderer
Sheriff

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: can private method be overriden/overloaded???
 
Similar Threads
main method
private final method
Q from javacross simulation
Static methods and variables.
Overriding