Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overriding doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overriding doubt" Watch "Method Overriding doubt" New topic
Author

Method Overriding doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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 ?



    Tell the difficulties that i am difficult.
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4342
        
        7

    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

    Joined: Feb 09, 2012
    Posts: 583

    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
    Bartender

    Joined: May 31, 2011
    Posts: 1776
    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

    Joined: Feb 09, 2012
    Posts: 583

    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

    Joined: Apr 06, 2010
    Posts: 4342
        
        7

    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

    Joined: Feb 09, 2012
    Posts: 583

    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
     
    Consider Paul's rocket mass heater.
     
    subject: Method Overriding doubt
     
    Similar Threads
    overridden method
    Is it not a bug on 111 K&B book ?
    Declaring exceptions in overriding methods
    Overriding problem
    exception