File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Override Method... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Override Method..." Watch "Override Method..." New topic

Override Method...

Ranch Hand

Joined: Dec 16, 2005
Posts: 103

The bellow is one of the rules for overriding a method.
�The access level can be less restrictive than that of the overridden method.�

If I try to run the following class it gives the error message. Pls let me know what am I missing.
� :14: amethod(int) has private access in Base0.amethod(iBase);�

class Base
private void amethod(int iBase)
class Over extends Base
public static void main(String argv[])
Over o = new Over();
int iBase = 0;
public void amthod(int iOver)
shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42
public void amthod(int iOver)
there is a spelling mistake it shud be amethod......
marc weber

Joined: Aug 31, 2004
Posts: 11343

private methods are not visible in subclasses, so they cannot be overridden.

With the typo corrected, this code should work. However, it's still not overridding the method -- it's just calling the method defined in Over.

The error message you posted would occur if the variable 'o' were defined as type Base, because then you would be trying to access a private member from outside the class.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Milind Patil

Joined: Oct 19, 2003
Posts: 26
Hi Ragu,

If check the code you have given, Base class's private method and child class's method name are different. You are using base class's instance to call super class's method which is actually private to super class.
That's why you are getting this error.

If you modify you base class's method signature then there wont be any proble executing your code.

Milind B. Patil - SCJP-1.4, SCWCD-5.0, SCBCD-5.0
I agree. Here's the link:
subject: Override Method...
It's not a secret anymore!