Results 1 to 6 of 6
  1. #1
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default Subclass Overridden Method throws Exception Rule?

    This might be a novice question, but still raises my eyebrow's

    Java Code:
    class Shape {
        public void calculateArea() throws Exception{
            //.............
        }
    }
     
    class Square extends Shape {
        public void calculateArea() {
        }
    }

    What is the reason behind having a subclass overridden method either omitting an exception or only throw a subclass of the superclass Exception.the reverse ?

  2. #2
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    What exactly is your question?

    Did you write this code?
    if you wrote it, then could you explain why you did that?

    and Shape i think is already a class inside of one of the java.awt.* packages.

  3. #3
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default

    Mate never mind the code. I just wrote some dummy class in the forum.

    I wanted to know why is there a rule as "A subclass overridden method can only throw the same exception or a sub class of the Exception that the superclass overridden method throws."

    Why such a rule?

  4. #4
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    i am not familiar with this rule, however it does seem to make sense, because when it's called, it's called with an expected error handling capability, so you need to keep that.

    Never heard this rule, that's more of a stab at it, but yeah i wouldn't question the rule.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    If calculateArea of Shape threw an exception ShapeException, and calculateArea of Square threw a SquareException which wasn't a subclass of ShapeException then what exception can I expect with:

    Java Code:
    Shape shape = new SquareException();
    try{
        shape.calculateArea();
    } catch (????) {
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hemanthjava View Post
    Mate never mind the code. I just wrote some dummy class in the forum.

    I wanted to know why is there a rule as "A subclass overridden method can only throw the same exception or a sub class of the Exception that the superclass overridden method throws."

    Why such a rule?
    Think of the Liskov Substitution Principle where a subclass can be used instead of the superclass. If a program uses the superclass it has to take into account that certain Exceptions can be thrown by a method and only those Exceptions. For that reason a sub class can not throw other Exceptions than those declared to be thrown by the superclass (or Exceptions extending the declared Exception).

    For the same reason a sub class can not override a method with less visibility than the method in the superclass, e.g. public in the superclass versus protected in the derived class.

    Summarizing the derived class has to act as if it were the superclass.

    kind regards,

    Jos

Similar Threads

  1. what are rule to overrid a method
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 03-30-2010, 07:24 AM
  2. JButton: getLocationOnScreen() throws Exception
    By ExceptionHandler in forum AWT / Swing
    Replies: 10
    Last Post: 02-15-2010, 01:38 PM
  3. Invoking a superclass version of a overridden method
    By CyberFrog in forum New To Java
    Replies: 3
    Last Post: 05-25-2009, 01:33 PM
  4. Main method with throws Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 02:48 PM
  5. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •