• 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

Overriding and Inheritance

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following classes.



Does this program cause a compiler error.Here the subclass overrides the method.Does the subclass method have to declare the IOException /
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please confirm you are using your real name.

Welcome to the Ranch

Please use the code button in future and make sure to indent code correctly; I have edited the post so you can see how much better it looks. Please try it for yourself and tell us what happens. If you get an error, tell us the details of the error.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer is no.
your code is running with no error too.
if you make an object from class A, and using the m1 method, you should surround it with try catch, but if you ovrriding it on class B and without throws clause when you use the method from an instance of class B, you shouldn't surround it with try catch.
Why you doesn't have to add the throws clause in the overriding methods? it's because the throws clause is only used if there is one or more statement on your methods need it. But if there isn't any statement that need to be throws, than it shouldn't.
 
p hasini
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rose
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch James Rose.

Please don't simply give that sort of answer. If you get the sort of question the poster could have answered, you simply excuse them the opportunity to do some work and find out for themselves. He could have written "javac B" and seen the errors (or lack of errors) in less time than it takes to read the posting.

The real explanation is that a subclass (overriding) method is a refinement of the superclass (Overridden) method. The rules about refinement are that a refinement (subclass method) is always feasible under whichever circumstances the refinee (superclass method) is feasible. If a method throws an exception, it means it is not feasible. The subclass method must not throw an exception when the superclass method would not have thrown an exception, but the Java compiler can only verify that for checked exceptions.
If the superclass method throws an exception, then it is not feasible, but there is nothing to stop the subclass method being feasible (ie running without exception) under the same circumstances.

So . . .

A subclass method mustn't add exceptions (but the compiler cannot check unchecked exceptions).
A subclass method may subtract exceptions.

And if you can understand that, you are doing well
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic