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 private and protected 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 "private and protected" Watch "private and protected" New topic
Author

private and protected

Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
can a private method be overriden by a protected method?

I thought no.. because private method will not be visible to any subclasses..


Regards
Shri..

SCJP 5.0
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

BTW, what happened when you tried it?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
Henry Wong wrote:BTW, what happened when you tried it?

Henry


i tried it like this :



the question was from free mock test

the question asked if a private method could be overriden by a private method.. i said no.. the software said yes you can and there was no explanation why..

but according what I know, if a method is declared private, then the subclass wont know if it even exists.. tell me the software is wrong here or how am i not understanding the question..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

the question asked if a private method could be overriden by a private method.. i said no.. the software said yes you can and there was no explanation why..


It is valid to have a method (private, protected, or public), in a subclass, with the same signature of a private method of the superclass. However, it is technically not overriding, as any method calls from the superclass to the method will be calling the private method of the super class.

Henry
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
Henry Wong wrote:However, it is technically not overriding, as any method calls from the superclass to the method will be calling the private method of the super class.


then i answered as no.. was i right or was i wrong ?

I thought I was right because it is not "overriding" the private method.. no one can override the private method then.. According the exact text in the question, the answer should have been "no".. right?

by the way, will there be such ambiguities in the "real exam" ?
Chitta Ranjan Mahato
Ranch Hand

Joined: Jun 20, 2009
Posts: 38
hello friend
As far i understood your question i trying to reply you.

When you declare a method private in the base or parent class it is not visible in the derived or child class actually here you defining a totaly new method.There is no concept of overriding here.

i am explaining through a example
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
Chitta Ranjan Mahato wrote:
When you declare a method private in the base or parent class it is not visible in the derived or child class actually here you defining a totaly new method.There is no concept of overriding here.


thanks for helping we came to that conclusion already in earlier part of this thread..
jeetendra Choudhary
Ranch Hand

Joined: Jun 02, 2009
Posts: 33
P.S This Is Called Redefining Not Overriding. Redefining is allowed.

Thanks & Regards
Jeetendra..!!


Preparing for SCJP...!!
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Chitta Ranjan Mahato wrote:When you declare a method private in the base or parent class it is not visible in the derived or child class actually here you defining a totaly new method.There is no concept of overriding here.


The @Override annotation comes in handy here. This code gives a compile error:


SCJP 6 || SCWCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private and protected