Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this overridden method?

 
Tony Sam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic