In overriding, in super class we declared a method as protected & in sub class we declared that method as private. what happens?

Questions by sadashivarao   answers by sadashivarao

Showing Answers 1 - 18 of 18 Answers

amit

  • May 15th, 2006
 

Error occured

  Was this answer useful?  Yes

crazy frog

  • May 15th, 2006
 

it wont compile, because access modifier of overridden methods should be less restrictive but not more restrictive,, u may declare as public ,,

since

more restrictive to less restrictive

private > default > protected > public

 

 

 

  Was this answer useful?  Yes

Nishi

  • May 17th, 2006
 

Both ans mentioned here are wrong....

Actully the code will compile because a less restrictive access specified method(as in Ques Protected) can be overridden by more restrivtive  access specified method(private).

Thanx

Nishi

  Was this answer useful?  Yes

abdul khalik

  • May 22nd, 2006
 

in ovveriding in subclass the access specifier should be less restrictive. if protected is used in super class then in sub class u can use public and protected both but not private. if u try to use private it will not be compiled.

Thanks

shivalak

  • May 23rd, 2006
 

Hi, Im sorry to say that Nishi is wrong and what crazy frog has commented is correct
You cannot reduce the visibility of the inherited method from superclass's method.
This is the order
(low visibility) ----------------------> (high visibility)
   private -> default -> protected -> public
e.g.
class A {
  private void method1() {
  // do something
  }
}
class B extends A {
   void method1() {
   // do something
   }
}
Here you are trying to reduce the visibility from private to default
which is not possible but the reverse works fine.
Thanks and Regards
Shiva.(shivalak@gmail.com)

  Was this answer useful?  Yes

Rizwan

  • Jul 31st, 2006
 

yes shiva its right what u said. bcoz when u override a method with a more restrictive access modifier it simply throws an error.

if u want any reference just go to sun certified book and have a look at 2 chapter

okey

thankx

rizwan

  Was this answer useful?  Yes

Vibs

  • May 14th, 2007
 

Hi Shiva,

Your explanation seems to be correct, but the example that you have given shows that the visibilty is going from low to high. i.e. From private to default. And as shown...

Private--> default--> protected--> public.

The example should work and reverse should not work.

Correct me if wrong.

  Was this answer useful?  Yes

vasu.funky

  • Oct 7th, 2007
 

Yes you are absolutely right. we can't have high restriced access specifiers in the overridden class. so if  the parent class method's allows protected then the child class methods can allow public but not private.

correct me if i am wrong.

  Was this answer useful?  Yes

well the answer to orignal question asked is -- compilation error

logic --> In a subclass we can't make the method more restrictive than what is there in the super class.

vinny

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions