• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
That new kid is a freak. Show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic