• 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

CertPal Q61 - Which will be OVERRIDDEN?

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the foll. code both the Fringe and Bread interfaces have the eat() method.
Which method is being overridden at line #3 ? Cannot understand why the code
gives Compiler Error. Bec' the implements clause has Fringe first does it not mean
that test implements eat () of interface Fringe ?


 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually you are overriding the eat method of both Fringe and Bread. This behavior is defined in the JLS. Basically since you are overriding both the eat methods, so the throws clause in the overriding method must have the most restrictive exceptions declared. Here since one eat method has Exception and the other MyException in the throws clause, so your eat method is allowed to throw MyException but not Exception...
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which method is being overridden at line #3 ?


Both methods are being overriden. You cannot throw Exception because eat() of interface Bread does not throw Exception. In this case you just can throw something that is IS-A RunTimeException(or a Error).
When you implement a interface you must implement all its methods. No matter how many interfaces has the method eat(), you must implement eat() for all the interfaces.

Bec' the implements clause has Fringe first does it not mean
that test implements eat () of interface Fringe ?


No, the order does not matter.
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Got it. Quick reply.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had just started watching the thread.
Prompt comes Ankit's reply. Lovely.

Thanks Ankit
 
It's a tiny ad only because the water is so cold.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic