• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Question about inheritance

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am preparing the OCA java 8 certification and I have a question about the following code




My question is about  the instruction g.play(), which method executes, play() of Soccer or play() of  Game?. According the solution to  the question, the code doesn't compile because the method executed is play() of Game and throws a Exception doesn't  catch.

According to the theory, the method invoked depends on the class of the actual object (Soccer).

Could anyone explain me because is executed play() of Game?
 
Creator of Enthuware JWS+ V6
Posts: 3290
296
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Quote-Your-Sources
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3290
296
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the rules when overriding a method when it comes to Exceptions?
 
Carlota Vina
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception have to be a subclass of overridden mehod
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3290
296
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of important rules when overriding a method:
  • The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception
  • The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method
  • The overriding method can throw narrower or fewer exceptions.


  • Which one does apply here, do you think?
     
    Carlota Vina
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think it is
    The overriding method can throw narrower or fewer exceptions.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3290
    296
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, play() of Soccer does declare fewer exceptions. So that is ok, right?

    The compiler doesn't know what happens runtime though, so it can only check method declarations. When it compiles the code it sees that you declare a Game g, and invoke the play method on g. According to the declaration of the method public void play() throws Exception it throws an Exception, right?

    That means, that you will have to handle (try...catch) or declare (throws...) that exception. What will be the outcome if you do handle the Exception?
     
    Carlota Vina
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't understand why it is executed Game play() it should be Soccer play(). The variable is Soccer
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3290
    296
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    a compiler doesn't execute anything, it can only check what is being declared.

    Game.play() declares an Exception, so you will have to deal with it. It doesn't matter whether Soccer.play() overrides or not.

    The compiler reads that g is a Game, which means that play() can thow an Exception.
     
    Carlota Vina
    Greenhorn
    Posts: 19
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot, I understand now
     
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Rules are as follows:
    If the superclass method does not declare an exception. If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
    If the superclass method declares an exception.
     
    Sayali pawar
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Frits Walraven wrote:What are the rules when overriding a method when it comes to Exceptions?



    The Rules are as follows:
    If the superclass method does not declare an exception. If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
    If the superclass method declares an exception.
     
    Marshal
    Posts: 65107
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Frits Walraven wrote:. . .

  • The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception
  • The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method
  • . . .

    This is, I think, an unfortunate situation, where the principles of good design and the rules of Java syntax conflict with each other. It is good design never to throw “new” exceptions of any kindIn a cert exam you will have to remember that the compiler won't notice the unchecked exception and therefore the code will compile all right. So you willget the mark for knowing that code will compile and run despite the bad design. If you are ever given that sort of code in an interview, you will have to say it will compile, but it isn&apost good code because of the risk of failure at runtime.
    Actually it won't compile; I shall leave you to find my mistake in the code.
     
    Carlota Vina
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Due to the method play() of Game throws a Exception, the main must to handle this exception(with a try-catch or with throws)
     
    Campbell Ritchie
    Marshal
    Posts: 65107
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carlota Vina wrote:Due to the method play() of Game throws a Exception, the main must to handle this exception(with a try-catch or with throws)

    No, it doesn't throw an exception; it declares it. If you have a plain simple Game object, then you must handle the exception. Come to thin kof it, you are correct; because your references are declared as Game, you must still handle the exception because the compiler cannot know whether a subclass does or doesn't declare Exception.
    I had forgotten that you are declaring your objects as Game: sorry.
     
    Enthuware Software Support
    Posts: 4331
    35
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This question is from Enthuware.

    Carlota, not sure if you are aware but if you click on the "Discuss" button on the question screen of ETS Viewer, it will take you to an existing discussion on that question (in this case, here: https://enthuware.com/forum/viewtopic.php?f=2&t=2053 ), which may save you time in getting your doubt cleared
     
    Carlota Vina
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot, I see the discuss screen now
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!