• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

overridding

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought private methods in a class cannt be overridden.
But chack this code and please explain. Or am I missing anything?
class Base
{
private void method(){}
}
class Sub extends Base
{
public static void main(String args[])
{
//call the method()
}
void method()
{
System.out.println("Inside Sub");
}
}

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you override a method, the new method cannot have an access modifer that is *more* private than the parent method. In this case, the parent method is private, and the child method is default, so this is okay. That being said, I'm not sure if this is really "overriding". Since the private method in the parent is not available to the child class, it seems that you would not really be overriding the method here. Instead, you would simply be making a new method which coincidentally has the same name as a method in the parent class. The two methods would not be otherwise related.
I'm sure someone will clarify this for both of us.
Gauth
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes private methods cannot be overriden. Private methods, as the name suggests, cannot be seen outside of the class. Hence you are free to define methods with the same name and signature in deriving classes. Then these 2 methods will be un-related.
What this also means that the derived class method does not have the usual restriction of checked exceptions...i.e. it's free to throw any checked exception irrespective of the base class method. Also note that private methods do NOT take part in polymorphism.
For eg. consider the code below:

The above code prints out "Inside Base".
[This message has been edited by Junaid Bhatra (edited July 29, 2000).]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic