• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding methods

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the question in my post up there ^
 
Aneeta Srini
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic