• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Abstract Class

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a class is declared abstract and contains abstract methods, can the subclass which is overriding the abstract methods has to follow the rules of overriding methods or not.
I mean if a abstract class has a method say
public abstract void(int i) throws SecurityException();
Do the subclass has to overide, to throw the same exception as super class or not
or it can be
public abstract void(int i) throws Exception;
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
overriding the abstract methods has to follow the rules of overriding methods or not
IMO, you have to.
Try a simple java program and you can verify.
Right now, I don't have JDK on this system, so
I can't give you an example.
regds.
- satya
 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a overriding method throws RuntimeException and sucbclass of RuntimeException, is it necessary even for overridden method to throw RuntimeException?
I feel answer is right. Do any one has any other opinion.
I think even if overridden method does not throw any exception and overriding method throw RuntimeException, it does not result in compiler error that "no method matching". It is completely legal right?
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is or would be legal because it is RuntimeException. not for checked ones. i mean we already know that errors and RuntimeExceptions need not be declared to be thrown in the first place itself. the compiler will not complain about it, though you will have problems in the runtime.
But for checked exceptions we have to follow the rule, that is, the overriding method has to declare the exception (from its overidden method) in its throw clause (or any of its superclass, ie the exceptions' superclass)
list, please second me on this to make sure we are learning the right things.

Originally posted by Arathi Rajashekar:
If a overriding method throws RuntimeException and sucbclass of RuntimeException, is it necessary even for overridden method to throw RuntimeException?
I feel answer is right. Do any one has any other opinion.
I think even if overridden method does not throw any exception and overriding method throw RuntimeException, it does not result in compiler error that "no method matching". It is completely legal right?

 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RuntimeException, it does not result in compiler error that "no method matching
Well, I was assuming you would comiple BEFORE running.

(or any of its superclass, ie the exceptions' superclass)
list, please second me on this to make sure we are learning the right things.


Mark:
I am a little on your stmt in the parantheses esp..."exceptions' superclass"...
IMO, the overriding method can (not necessaryly)throw either the same Exception as the declared in the (abstract)parent class method or any Exception which is a subclass of the Exception declared in the (abstract)parent class method.
As I mentioned I am currently on a system without a JDK, so I can't illustrate this stmt with an example which, IMO is the best way to understand. I will try and get an example after getting home, hopefully.
regds.
- satya
ps: This is a very fundamental rule for over-riding and any std. book will explain it, pl. refer to one.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The golden rule is that a method overriding another method that throws an exception, must throw either the same exception or an exception that is subclassed from the original method exception or it can throw NO exceptions at all. anything else, and you have a problem in your code.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing beats a question like code.

all compiles(jdk1.3) and runs without any problem...
hope this helps!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic