File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes private method override Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "private method override" Watch "private method override" New topic
Author

private method override

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
I inherit a base class.Base class has one private method.

In the derived class I can override private method.How it is possible private means only particular class has the access ,how a derived class has the access?
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
When you are overriding the method from parent class, its not called "accessing" - you are basically modifying the functionality of the base class - completely changing it or enhancing it.
Accessing a private function of the super class would still give compile time error. narrowing down the privilege of the overridden method in child class will also give you compile time error.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

jacob deiter wrote:In the derived class I can override private method.

No you can't - in order to override a method, that method must be visible in the overriding class. Therefore, private methods can never be overridden.
Instead, you are simply implementing a new method that happens to have the same signature (name + parameters + return type).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
narrowing down the privilege of the overridden method in child class will also give you compile time error.


can you give some example?

in your reply, you says that private method of base class can be overridden in sub class.
if it so then protected method of base class can not be overridden in sub class ??
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
you are simply implementing a new method that happens to have the same signature (name + parameters + return type).


I got the Point
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

jacob deiter wrote:
narrowing down the privilege of the overridden method in child class will also give you compile time error.


can you give some example?

in your reply, you says that private method of base class can be overridden in sub class.
if it so then protected method of base class can not be overridden in sub class ??

What amitabh meant is the following:
- a public method must be public in any sub class.
- a protected method must be protected or public in any sub class
- a method with default access must have default access or be protected or public in any sub class

In other words, you can't reduce the visibility.

This is not really related to your initial question though.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: private method override
 
Similar Threads
access modifier ??
Ambiguous question in Naveens test
private method overiding
method overriding and overloading
Private methods' inheritance