• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Exam cram( overriding)

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In exam cram on pg. 131, method in parent class is,
public boolean readTest(String x) throws IOException
The Java compiler will allow the overriding subclass method to be defined as throwing IOException, throwing one or more subclasses of IOException, or not throwing any exceptions.
My question is regarding not throwing exceptions at all. I have read in Barry Boone's Book( pg. 200, answer to Ques. 7), that
When you override a method, you must also indicate the exceptions the overridden method will throw. I have checked it by compiling the parent class & subclass. It gives error if overriding method doesn't indicate the exception thrown by
overridden method which is contradictory to exam cram. Is there something I am not getting?
This is the Parent class :
class B{
public static void main(String args[]){
B b = new B();
b.test(args);
}
void test(String []args){
String s;
if(args.length == 0)
s = new String("don't throw");
else
s = args[0];
try{
method(s);
System.out.println("no exception");
}
catch(MyException e) {
System.out.print("caught");
}
}
void method(String s) throws MyException{
if(s.equals("throw"))
throw new MyException();
else
return;
}
}
class MyException extends Exception{}
----------------------------------------------------------------
This is the subclass which overrides method(String s):
class C extends B{
public static void main(String []args){
C c = new C();
c.test(args);
}
void method(String s) throws MyException{
if(s.equals("yes"))
throw new MyException();
else
return;
}
}
Please help. Thanks.
KN
 
KN
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The above set of Parent & subclass compiles fine but if the overriding method doesn't throw the exception i.e, declared as
void method(String s){
subclass doesn't compile. Why?
Thanks,
KN
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could it be that you area throwing an exception inside the method, then you are forced to declare the throws statement.
Try with:
void method(String s) {
if(s.equals("yes"))
//throw new MyException();
//else
return;
}
It compiles fine!
Then if you have time look at my question:
http://www.javaranch.com/ubb/Forum24/HTML/002675.html
 
KN
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marcela,
I was wondering about the same thing. I think you are right.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic