• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I declare a superclass method whose modifier is 'public' as private in subclass while overriding?
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try it? What happened?
 
satish sathineni
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kri shan

While Overriding the superclass method in the sub class

1. The access modifier of the subclass overriding method should be
below the acess modifier level of the super class method acess modifier.

ex :: public void MethodA() // super class method

ex :: protected void MethodA() // sub class method

The accessmodifier can be descending when compared to super class acess modifier.
Private,Protected,default,Public


 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kri shan:
Can I declare a superclass method whose modifier is 'public' as private in subclass while overriding?


No. In simple words, the subclass method should be atleast as visible as it's corresponding superclass method. This is the minimum requirement. If the superclass method is public, then you have no other choice but to have your subclass method too as public.
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satish_kumar:
Hi kri shan

While Overriding the superclass method in the sub class

1. The access modifier of the subclass overriding method should be
below the acess modifier level of the super class method acess modifier.

ex :: public void MethodA() // super class method

ex :: protected void MethodA() // sub class method

The accessmodifier can be descending when compared to super class acess modifier.
Private,Protected,default,Public




When you say "below" are you refering to some hierarchy or narrowness/width of the scope? If it is the latter, then it is not correct. You cannot narrow the scope of a subclass from that of its superclass. If it is the former, then is the hierarchy what you have listed in the last line of your post? In that case you are mostly right. A subclass of a public super class can have the default scope.
 
beth ana
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rules of method overriding:

SuperClass SubClass
public must be overriden public.
Protected can be public, protected
default/no modifier can be public, protected, default/no modifier
private can be public, protected, default/no modifier
static combination must be static combination
abstract must be overriden
final cannot be overriden
native don't know - try out

There are rules of overriding in case of excepion handling for checked & unchecked exceptions.
Let me If I am reasonable.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amended table for method overriding:

I think I'm right with these, but there's almost always something you miss when you do things from memory... :roll:

Jules

[ August 04, 2004: Message edited by: Julian Kennedy ]
[ August 04, 2004: Message edited by: Julian Kennedy ]
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In simple words, a subclass must provide more access if it changes the access of a method.
There is no reason to give less access as the method can be accessed by a reference to the superclass with more access.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!