File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract Class" Watch "Abstract Class" New topic
Author

Abstract Class

Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
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;


Arathi<br />Sun Certified Java Programmer
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
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

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jun 03, 2000
Posts: 5040
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

Joined: Jan 07, 2001
Posts: 29
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.


A Software life cycle can be greater than its Developer's
mario jones
Greenhorn

Joined: Jun 14, 2001
Posts: 7
Nothing beats a question like code.

all compiles(jdk1.3) and runs without any problem...
hope this helps!


-Mario
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract Class
 
Similar Threads
Overriding methods & Exceptions
Greedy :)
How to get 100% in scjp 5.0 ?
few questions
Implementing abstract methods in the concrete subclass