File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in overriding

 
Shruti Rao
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Vinod Tiwari
Ranch Hand
Posts: 466
1
IntelliJ IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congrats
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
Notepad
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rajeev Rnair
Ranch Hand
Posts: 308
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 !


 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic