Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Overriding Methods

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Overriding Methods

    There is a rule while overriding methods i.e.
    "overriding method MUST NOT throw new or broader checked exceptions but MAY throw fewer or narrower checked exceptions or any unchecked exceptions ".

    May i know for what reason this restriction is there in Java??


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Overriding Methods

    You have a class A that contains someMethod() which throws a NullPointerException. So any code calling that method knows it must catch the NPE. You then have a class B that extends A and overrides someMethod()- but that someMethod() throws an IllegalArgumentException instead. That results in this code-

    A bInstance = new B();
    try{
       bInstance.someMethod();
    }
    catch(NullPointerException e){
       //whatever
    }

    Do you see what's wrong with allowing that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Overriding Methods

    Please explain in more detail.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Overriding Methods

    Quote Originally Posted by tcstcs View Post
    Please explain in more detail.
    What? How much more detail do you need? What didn't you understand? Did you write a similar program yourself to try it out?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Overriding Methods

    Code that calls a method that is declared to throw exceptions must be explicitly coded to either handle those exceptions or itself be declared to throw them on. If you could override a method in a subclass to throw different exceptions, code that expected the superclass implementation could receive a subclass implementation and try to call that method, but wouldn't know about the different exceptions, so wouldn't know to handle them or throw them on. That would break the rule that they must be explicitly handled or thrown, so it isn't allowed.

    The exceptions declared to be thrown by a method are part of the method signature. When you override a method, you must use the same method signature.

    Check out Exceptions in Java.
    Last edited by dlorde; June 1st, 2011 at 12:42 PM.

  6. The Following User Says Thank You to dlorde For This Useful Post:

    tcstcs (June 2nd, 2011)

  7. #6
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Overriding Methods

    Thank u dlorde. Got one more material with same explanationas urs
    Handle exceptions in overriding methods

Similar Threads

  1. Inheritance and Overriding help!
    By Knserbrave in forum Object Oriented Programming
    Replies: 4
    Last Post: February 24th, 2011, 01:46 PM
  2. Overloading vs Overriding
    By nickypass in forum Java Theory & Questions
    Replies: 1
    Last Post: October 17th, 2010, 01:53 AM
  3. Overriding methods declaration problems
    By TurboTuring in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 9th, 2010, 11:23 AM
  4. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM
  5. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM