File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is this overridden method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is this overridden method?" Watch "Is this overridden method?" New topic
Author

Is this overridden method?

Tony Sam
Ranch Hand

Joined: Nov 19, 2001
Posts: 32
import java.io.*;
class Base{
private void amethod(int iBase) throws IOException{
System.out.println("Base.amethod");
}
}
class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
try{
o.amethod(iBase);}
catch(Exception e){}
}
public void amethod(int iOver) throws Exception{
System.out.println("Over.amethod");
}
}//end here
This program compiles OK,but i wonder whether this is a overridden method. if yes,this must be compile error! A litter confused, Please tell me more about overridden and overloaded method!
Thanx in advance!

Tony Sam<BR>One want to be a SCJP...
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Originally posted by Tony Sam:
import java.io.*;
class Base{
private void amethod(int iBase) throws [b]IOException
{
System.out.println("Base.amethod");
}
}
class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
try{
o.amethod(iBase);}
catch(Exception e){}
}
public void amethod(int iOver) throws Exception{
System.out.println("Over.amethod");
}
}//end here
This program compiles OK,but i wonder whether this is a overridden method. if yes,this must be compile error! A litter confused, Please tell me more about overridden and overloaded method!
Thanx in advance![/B]


U guessed right Tony! amethod(int) being private in base class can't be overriden. Private methods are private to class only so they are not inheritaed. Therefore,
0.amethod() actually calls Over class's amethod().
If u change base class method as...
public void amethod(int iBase) throws IOException{}
then the version in over class becomes an overriden method. so compiler raises error as...

Over.java:15: amethod(int) in Over cannot override amethod(int) in Base; overridden method does not throw java.lang.Exception
public void amethod(int iOver) throws Exception{

This is because the overriden method can throw anly subset of exceptions thrown by base class method.
Hopr this helps!!
Rashmi

[This message has been edited by Rashmi Gunjotikar (edited December 02, 2001).]
 
Consider Paul's rocket mass heater.
 
subject: Is this overridden method?