Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding methods

 
Aneeta Srini
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given a method that does'nt declare exceptions, can you override that method in a subclass to throw an exception?

 
Sethu V
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Aneeta Srini
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the question in my post up there ^
 
Aneeta Srini
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic