Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Overriding doubt

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certification Objective —Overriding / Overloading (Exam Objectives 1.5 and 5.4)

The rules for overriding a method are as follows:


  • The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.


  • In my program given below, my overriding method will never throw checked exception (IOException) then it should work without checked exception declaration according to above definition but i got error, so what does it mean of above line ?


     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's fine for Checked to override Car without declaring the exception, which is what those rules are describing.

    But on line 19 you're calling show() on a reference of Car type. Which means that, as far as the compiler is concerned, the method might throw an exception. The actual type of the object isn't important here - the compiler uses the reference type to work things out.

    So, since IOException is a checked exception, the compiler will insist it's either caught or declared as thrown.

    If you called show() on the Checked variable, then you wouldn't get the error, because the compiler knows that can't throw a checked exception.
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Matthew Brown wrote:It's fine for Checked to override Car without declaring the exception, which is what those rules are describing.

    But on line 19 you're calling show() on a reference of Car type. Which means that, as far as the compiler is concerned, the method might throw an exception. The actual type of the object isn't important here - the compiler uses the reference type to work things out.

    So, since IOException is a checked exception, the compiler will insist it's either caught or declared as thrown.

    If you called show() on the Checked variable, then you wouldn't get the error, because the compiler knows that can't throw a checked exception.


    Thanks Matthew
    Please can you give me example for this line, "an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares."
     
    John Jai
    Rancher
    Posts: 1776
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You already have an example in your first post. The show() method in Car throws an IOException but the show() method in Checked does not throw any Exception.

    show() in Car - overridden method
    show() in Checked - overriding method
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Jai wrote:You already have an example in your first post. The show() method in Car throws an IOException but the show() method in Checked does not throw any Exception.

    show() in Car - overridden method
    show() in Checked - overriding method


    Thanks
    but i think, as this program can't compile as compiler gets confused that show method has IOException declaration which is not in overriding method, this is contradiction about rule, because when we call show method on checked class object then it does satisfy that rule ,but when we call on Car reference then it will be a compile error so in this condition there is contradiction about that rule, that rule fails in second condition that's why i asked that how this rule is correct .

     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think you're misunderstanding the error that the compiler is giving you. Comment out the main method, and it should compile fine. That shows that overriding like that is allowed.

    As I said - your code is failing to compile because you're calling a method that might throw a checked exception without catching it. The problem is on line 19. Change line 19 to carobj.show(); and it will also be OK.
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Matthew Brown wrote:I think you're misunderstanding the error that the compiler is giving you. Comment out the main method, and it should compile fine. That shows that overriding like that is allowed.

    As I said - your code is failing to compile because you're calling a method that might throw a checked exception without catching it. The problem is one line 19. Change line 19 to carobj.show(); and it will also be OK.


    Thanks Matthew
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic