• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling super.method() in subclass's overridn method

 
dilan alex
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why super.eat(); generates an error ??
I cannot understand because FileNotFoundException is a sub class of IOException.
I can add try catch clause and eliminate this error by catching IOException or throwing an IOException in Animal1.eat() method.
Still I have a problem why super.eat() generates an error ???

Any help will be appreciated.

Thanks in advance,
Dilan Alex.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Animal1{

protected Animal1() {

}

void eat()throws IOException{System.out.println("Animal Eat....");}

}
}

public class Dog extends Animal1{

public void eat() throws FileNotFoundException{
super.eat(); //this line generates an error
System.out.println("Dog Eat.....");
}
}



look at your Brackets here



Mike
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dilan , please don't edit any of your posts after a response, as that takes the response out of context.

When seeking help with an error, always quote the full compiler or runtime error text. That will get you better help sooner.
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dilan welcome to Ranch

if you want to over ride you can not throw the Subclass of IOException
so you can throw IOException in the sub class Dog for eat method

or if you are not overriding then you have to catch IOException, because Dog class eat method is thows only FileNotFoundException which is Sub class of IOException

 
dilan alex
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chiranjeevi Kanthraj wrote:dilan welcome to Ranch

if you want to over ride you can not throw the Subclass of IOException
so you can throw IOException in the sub class Dog for eat method

or if you are not overriding then you have to catch IOException, because Dog class eat method is thows only FileNotFoundException which is Sub class of IOException



When we overriding a method its legal to throw an Exception in overriden method which is a subclass of overriding method's Exception.
but here only problem is I'm calling super.eat();

Finally i think like this..

super.eat(); throws IOException and Dog class eat() method throws FileNotFoundException, So FileNotFoundException cannot fit into the IOException because IOException is the super class.

am I correct ??

Regards,
Dilan Alex.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

No, you are not correct. Because FileNotFoundException is a subclass of IOException, it is permissible to declare it there.
You haven't told us the details, for example: what sort of error are you suffering?
 
dilan alex
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again

No, you are not correct. Because FileNotFoundException is a subclass of IOException, it is permissible to declare it there.
You haven't told us the details, for example: what sort of error are you suffering?


Hi all,

here is the error i'm getting,



Regards,
Dilan.
 
dilan alex
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again

No, you are not correct. Because FileNotFoundException is a subclass of IOException, it is permissible to declare it there.
You haven't told us the details, for example: what sort of error are you suffering?


ok I want to change it like this..

super.eat(); throws IOException and Dog class eat() method throws FileNotFoundException, So IOException cannot fit into the FileNotFoundException because IOException is the super class.

I think now it is complete

Regards,
Dilan.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right, but am not sure you have expressed it clearly.

What happens is that the superclass method declares IOException (IOE). That exception is "transported" to the subclass (overridden) method by the super.eat() call. Now the subclass might (so the compiler thinks) suffer an IOE which isn't a FileNotFoundException (FNFE). It can deal with FNFEs because it declares them, but cannot deal with other IOEs. So you must deal with the IOE somewhere around that super.eat() line. If you deal with it by means of a try-catch, you will get a different compiler error. Try it and see.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again

No, you are not correct. Because FileNotFoundException is a subclass of IOException, it is permissible to declare it there.
You haven't told us the details, for example: what sort of error are you suffering?

Dilan is right. Usually the FileNotFoundException would be good, but the call to super.eat() can throw an IOException and so the overridden method must either also declare to throw IOException or catch it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic