• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Overididng doubt

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can we have more restrictive modifier when we override a method for
eg-If a method has public access modifier and make it protected when we override.
if we can do then please explain me and if we cant then why?
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya,
We can't have like that. The overriden method's doesnot contain more restrictive return type. It is one of the rules we have to follow.
For more information please go to Kathy's book.
All the best.

Sashi
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Divya Gehlot:
Can we have more restrictive modifier when we override a method

No.
What should be the result of this program? Parent promises that there is a function named doIt which everyone can call. But Child says: Forget it. Only subclasses have the right to call this method.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, ranchers,

Div asked:

if we cant then why?


Another example.
I don't take public<-->protected but public<-->default, as it is the same thing, but maybe easier to explain.

Imagine, that it were allowed to reduce visibility when overriding and you have two classes in two packages:
Then the return of the method becomes inconsistent, depending on from what package the method is called.
From package ranch, the overridden method is visible, and the call on an Horse object, e.g. aHorse.whatItEats() returns "hay", but when an object of the same type is called from any other package, the overridden method is invisible and anotherHorse.whatItEats() would return "general food" of the super class.


Because of these inconsistencies reducing visibility in overriding cannot be a good thing.


Yours,
Bu.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a perfect explanation Bu

Just to add what Bu said, if at all the overriding method is more restrictive than that of the overridden method, it would seem to collapse the contract!

Because, the parent method (overridden) guarantees a certain behavior which should atleast be preserved by its subclasses when overriding such methods.

Does make some sense?
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic