aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding methods" Watch "Overriding methods" New topic
Author

Overriding methods

Aneeta Srini
Greenhorn

Joined: Jul 23, 2002
Posts: 16
Given a method that does'nt declare exceptions, can you override that method in a subclass to throw an exception?

Sethu V
Greenhorn

Joined: Aug 06, 2002
Posts: 10
No u cant do that
u can try it out writing a small program
Originally posted by aneeta s:
Given a method that does'nt declare exceptions, can you override that method in a subclass to throw an exception?

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Do you mean a checked or an unchecked exception?
By the way you guys, my sword is glowing, the "Naming Policy" Gru is around here somewhere...
[ August 13, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Aneeta Srini
Greenhorn

Joined: Jul 23, 2002
Posts: 16
I tried the following code :
class Test
{

void display()
{
System.out.println("Displaying");
}

}
class OneMoreTest extends Test
{
void display() throws ArrayIndexOutOfBoundsException
{
char a[] = {'a','n','i','t','a'};
System.out.println(a[5]);

}
public static void main (String args[])
{
OneMoreTest t = new OneMoreTest();
t.display();
}
}
When i use ArrayIndexOutOfBounds exception..it compiles but when I use IllegalAccessException..it does not? Please guide me.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
By the way you guys, my sword is glowing, the "Naming Policy" Gru is around here somewhere...
and here he is
Sethu,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
See the question in my post up there ^
Aneeta Srini
Greenhorn

Joined: Jul 23, 2002
Posts: 16
Barry I did read your question ! Am I getting this right? Given a method that does not declare any exceptions, you cannot override that method in the subclass to throw a checked exception.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You have found the solution to the problem!
The method in the subclass may only throw a subset (including none) of the checked exceptions declared to be thrown by the overridden method.
-Barry
[ August 13, 2002: Message edited by: Barry Gaunt ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
And Aneeta S,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
Tom <- shameless stealer of other people's posts!
[Links added by Val]
[ August 13, 2002: Message edited by: Valentin Crettaz ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding methods
 
Similar Threads
Sharpen your Pencil - Page 323 in HFEJB
NX: When should I throw DuplicateKeyException?
Changin font in a JTextArea
garbage collection
Finalization Method