• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in overriding

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 466
1
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congrats
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !


 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic