This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

overriding question  RSS feed

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*

compiling code below was successful, i had thought the method overriding by B.doo() would fail without the declaration "throws Exception".

What's the overriding rule regarding it?

Thanks!

*/


class A {

void doo() throws Exception {}

}

class B extends A {

void doo() {}

}
 
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,

When you override a method, make sure for 2 things regarding exceptions, is that your overriden method cannot throw broader checked exception and if it dosn't throw any checked exceptions also it works. It can throw any run time exceptions.
 
Bob CHOI
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just found the rule:

overriding method may throw fewer or narrower checked exceptions, or any unchecked exceptions.
 
Bob CHOI
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jothi!
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the following cod eyou will get error

class A {

void doo() throws Exception {System.out.println("sys");}

}

public class B extends A {

void doo() {System.out.println("sys1");}

public static void main(String s[]) {

A a=new Test();

a.doo();//unreported exception error

Test t=new Test();
t.doo();
}
}

ie Overriding method not declared the exception, declared by the super class
 
Suguna Gollapally
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {

void doo() throws Exception {System.out.println("sys");}

}

public class Test extends A {

void doo() {System.out.println("sys1");}

public static void main(String s[]) {

A a=new Test();

a.doo();

Test t=new Test();
t.doo();
}
}
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suguna,

Consider the code below,



The above works fine and I'n not saying void test() throws Exception in my subclass. It works just fine. The point is if any method says to the world that it might throw an exception, then callers of that method should make arrangements to handle any exceptional condition that might occur. It has got nothing to do with overriding. Hope you understood the concept.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Jyothi,

Thanks for your deatiled answer... so you say compiler wont check whether the overriding method throws exception or not,but when we call the overridden method,programmer should handle it as it throws checked exception other wise it would be an unreported exception and compiler spit an error...am rite?

Harish
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!