• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I declare a superclass method whose modifier is 'public' as private in subclass while overriding?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try it? What happened?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic