File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Class

 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • 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;
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Maitham H
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
mario jones
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing beats a question like code.

all compiles(jdk1.3) and runs without any problem...
hope this helps!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic