• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

SCJP book - Overriding/Overloading objective

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Sierra/Bates book, in the Overriding objective (chapter 2), I have a question. This objective says that the overriding method in the subclass can have fewer checked exceptions than the overridden method in superclass. But "Exam Watch" after it goes on to say that a checked exception must be declared in overriding method in subclass if there is a checked exception in overridden method in superclass. Isn't this a contradiction?
[ July 01, 2007: Message edited by: Sachin Kapoor ]
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i do not think so, i think it is trying to say that in SUB CLASS overridden Method(),you can declared less exceptions, but not the above to the one which is declared in the SUPER CLASS method.

e.g. A {
m1() throws Exception {}
}
B extends A { m1() throws FileNotFound Exception {}

A {
m1() .//nothing in here
}

B { m1() throws Exception .// should not be here

- assuming all other rules for a valid and legal overriding
- chintan
 
Sachin Kapoor
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the first post was confusing, here's the code from K&B book. Can someone follow through this code with me? Why is there a compilation error?


[ July 01, 2007: Message edited by: Sachin Kapoor ]
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sachin, have a look at the following code

class EXCEP1 {
void m1() throws Exception {System.out.println("Howdy");}
}

public class EXCEP extends EXCEP1{

void m2() {System.out.println("Hello");}

public static void main(String []args){
try{


EXCEP1 ee = new EXCEP();
ee.m1();
} catch(Exception e){}
}
}


- it does compile, i think in your code the error is of NOT handling the exception rather declaring exception, so if you invoke a method that throws Exception then caller has to handle it or the method itself should provide the handling of whatever type occurs by that method, so in your code if you give TRY/CATCH block then it will compile, you would have 3 possibilities for that either in SUPER , or in SUB or in the class wherever you are invoking the method.
- regards - chintan
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic