Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Method Overriding doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?


     
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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."
     
    Rancher
    Posts: 1776
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic