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 Doubt in overriding 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 "Doubt in overriding" Watch "Doubt in overriding" New topic
Author

Doubt in overriding

Shruti Rao
Greenhorn

Joined: Feb 01, 2010
Posts: 9
Hi,
I have a doubt in the rules of overriding from K&B book. Can anyone of you all explain me the meaning of the following two lines:

1) The access level cant be more restrictive than the overridden methods.
2) The access level CAN be less restrictive than that of the overridden method.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

The access level cant be more restrictive than the overridden methods.


This means if the super class method is declared protected then the overridding method in the sub class cannot be either private or default access level. It must have either protected or public access level.


Life is easy because we write the source code.....
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 466
    
    1

Congrats
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2268
    
    3

Vinod Tiwari wrote:Congrats


Congrats for the doubt?!

Let's slide this over to the SCJP forum, where I think this question fits better.
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91


I have a doubt in the rules of overriding from K&B book. Can anyone of you all explain me the meaning of the following two lines:

1) The access level can not be more restrictive than the overridden methods.
2) The access level CAN be less restrictive than that of the overridden method.



Let me make you clear that both the statements above rule 1 and rule 2 have same meaning.
The following code elaborates the rules actually only rule

Have a look at access specifiers to the doIt() method it is possible the compiler is happy with the above code.
hope you go through code.


Thanks & Regards
Sumit Kothalikar
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308



Please check carefully. doit() is declared as "private" method in class "A"
Hence it is NOT visible in sub classes
all doIt() methods in sub class are NOT overriding the class "A" method, it is just a re definition of method. Hence no RULES of overriding apply here !



SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in overriding