aspose file tools*
The moose likes Beginning Java and the fly likes calling super.method() in subclass's overridn method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling super.method() in subclass Watch "calling super.method() in subclass New topic
Author

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

dilan alex
Greenhorn

Joined: Sep 17, 2011
Posts: 27


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

Joined: Aug 08, 2010
Posts: 210

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

Joined: May 03, 2008
Posts: 4664
    
    5

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.

luck, db
There are no new questions, but there may be new answers.
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

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


-Chiru
dilan alex
Greenhorn

Joined: Sep 17, 2011
Posts: 27
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Sep 17, 2011
Posts: 27
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

Joined: Sep 17, 2011
Posts: 27
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling super.method() in subclass's overridn method