The access level *cannot be more restrictive* than the overridden method's
access level.
However the access level *can be less restrictive* than the overridden
method's access level.

Here is an example:

Lets say you are overriding: public void doSomething{}
Well you cannot make it:     private void doSomething{}  because declaring
the method private is more restrictive than declaring it public.

As for:

The overridden method should not throw checked exceptions that are new or
broader than the ones declared by the overridden method.
The overriding method can throw narrower or fewer exceptions than the
overridden method.

I am less sure about this but I think my example will work:

If public void doSomething(throws nullPointerException E){} before being
overridden, then the overridden
method cannot throw a simple (Exception E) because it would catch any type
of exception, not just the nullPointerException.

Conversely the parent method, public void doSomething(throws Exception E),
could be overridden with public void doSomething(throws nullPointerException
E){}
because it is a more specific or narrower exception type.

Hope this helps.

Chris


On Oct 18, 2010 9:47 AM, "Nirmal Kumar" <nirmal.h...@gmail.com> wrote:

Hi All,

Can anyone explain the following Method Overriding Rules in Java:

The access level *cannot be more restrictive* than the overridden method's
access level.
However the access level *can be less restrictive* than the overridden
method's access level.

The overridden method should not throw checked exceptions that are new or
broader than the ones declared by the overridden method.
The overriding method can throw narrower or fewer exceptions than the
overridden method.

Please give an example if possible.

Thanks,
Nirmal



<http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en>

-- 
You received this message because you are subscribed to the Google
Groups "Java EE (J2EE) Programming with Passion!" group.
To post to this group, send email to
java-ee-j2ee-programming-with-passion@googlegroups.com
To unsubscribe from this group, send email to
java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en

Reply via email to