Win a copy of Spark in Action this week in the Open Source Projects 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

interfaces and default methods

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


line 20 shows an error
but if i make a change on line 10 as : interface abcd extends abc , then it compiles fine and gives the output (abcd) by executing abcd interface's version of method m().
can somebody explain what exactly is happening on the lines 10 and especially 20 ?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:can somebody explain what exactly is happening on the lines 10 and especially 20 ?


In the current code, class c2 has two versions of the method m(): one from each interface it implements. So iit's impossible for the compiler to know which version of the method m() must be used in class c2 and that's why you give the compiler. One possible solution would be to implement the method m() in class c2. And that's probably the most common used solution (because you can keep the current class and interface hierarchy).

If you change the interface hierarchy (let interface abcd extend from interface abc), you don't have two versions of the method anymore. The method m() in interface abc is overridden by the method m() in interface abcd. And that's why you don't get a compiler error anymore and the program can be executed.

Hope it helps!
Kind regards,
Roel
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it is a third class that is inheriting form abc and abcd right ?

so how does it have only one version of m() ?
it has inherited from both abc and abcd right ? abcd inherits from abc and has overriden m() so it provides only one version of m().

but c2 class is inheriting from abc and abcd , so it will have one form abc and one from abcd right
 
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:
but c2 class is inheriting from abc and abcd , so it will have one form abc and one from abcd right



Default methods are *not* a form of multiple inheritance. It may look like multiple inheritance, but it is not multiple inheritance.

If your class doesn't implement a method of the interface (or inherits it from its superclass), then the compiler will use the default method from the interface.

If there are multiple matching default methods, and there is no way to disambiguate, as in your first case, then you get a compiler error.  If there are multiple matching default methods, and one is from the subinterface of the other interface, then the compiler will choose the one from the subinterface; as shown in your second case.

Henry  
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot henry !      
 
This tiny ad will self destruct in five seconds.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic