• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Is this overridden method?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic