wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Override Method..." Watch "Override Method..." New topic
Author

Override Method...

RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
Hello,

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.
�Over.java :14: amethod(int) has private access in Base0.amethod(iBase);�




class Base
{
private void amethod(int iBase)
{
System.out.println("Base.amethod()");
}
}
class Over extends Base
{
public static void main(String argv[])
{
Over o = new Over();
int iBase = 0;
o.amethod(iBase);
}
public void amthod(int iOver)
{
System.out.println("Over.amethod()");
}
}
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
Sheriff

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
sscce.org
Milind Patil
Greenhorn

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: http://aspose.com/file-tools
 
subject: Override Method...
 
Similar Threads
doubt private method
[B] a private final method can be overridden!!!! [/B]
Marcus Green's Exam #1 Question 59
Private methods overridden?
private method...