• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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: 9708
43
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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic