It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overididng doubt 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 "Overididng doubt " Watch "Overididng doubt " New topic

Overididng doubt

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
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?

SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Shashidhar Yarabati
Ranch Hand

Joined: Jun 17, 2007
Posts: 175
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.


Shashidhar<br /> SCJP 1.5<br /> SCWCD 5.0
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Divya Gehlot:
Can we have more restrictive modifier when we override a method
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.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

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?

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
I agree. Here's the link:
subject: Overididng doubt
It's not a secret anymore!