• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

implementing interfaces with default methods

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm reading the OCA study guide by Jeanne Boyarsky and Scott Selikoff.
At Chapter 5, page 278 it says:
"If a class implements two interfaces that have default methods with the same name and signature, the compiler will throw an error."
Then the book states there is one exception to this rule and lists it;
"if the subclass overrides the duplicate default methods, the compiler will compile without issue-..."

I'd think there is another exception; if one interface extends the other then the compiler will run without issue.
Consider the following code:


This compiles without error and prints 2.
Maybe this could be expected but earlier on there is an example about two interfaces with each a default methods where the two methods have the same name and signature and being siblings and possible ambiguity hence a compile error.
Would this case be worth mentioning, possibly briefly as
"If a class implements two sibling interfaces that have default methods with the same name and signature, the compiler will throw an error."
Maybe even taking into account the listed exception:
"If a class implements two sibling interfaces that have default methods with the same name and signature without overriding them, the compiler will throw an error."
accompanied by the examples?
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Corneth wrote:"If a class implements two interfaces that have default methods with the same name and signature, the compiler will throw an error."

Here default methods of those two interfaces having same signature are inherited ergo compiler finds ambiguity which one is going to be used by Cat class ( method of Walk Or Run ) so gives compile time error.

Then the book states there is one exception to this rule and lists it;
"if the subclass overrides the duplicate default methods, the compiler will compile without issue-..."  


Here subclass i.e. Cat provides It's own implementation of method getSpeed() by overriding It, now compiler knows which version of getSpeed() method (method provided by Cat class) is going to be used so compiles successfully.
But

I'd think there is another exception; if one interface extends the other then the compiler will run without issue.
Consider the following code: This compiles without error and prints 2.

I don't think so because there is no ambiguity while calling getSpeed method in above example because getSpeed method of interface i1 is overridden by getSpeed method of i2 since both have same signature, therefore compiler knows getSpeed method of interface i2 is going to be used so gives no compile time error and prints 2.

 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO

"If a class implements two interfaces that have default methods with the same name and signature, the compiler will throw an error."

It should be
""If a class implements two interfaces that have default methods with the same signature, the compiler will throw an error."  since method signature comprises name and parameter type(if any)

JLS 10, 8.4.2. Method Signature wrote:Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any)

 
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this is a bit confusing to me. How does the compiler know youre calling i2's method? Is it because you are implicitly inheriting i1 regardless of if you don't state it? It seems to me that if you declare both interfaces your code would inherit both methods unlike if you only implemented i2, then your class would inherit the overriden version of the method.

-Zach
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zach Rode wrote:So this is a bit confusing to me. How does the compiler know youre calling i2's method? Is it because you are implicitly inheriting i1 regardless of if you don't state it?

My apology for creating confusion. I didn't notice InterfaceTest implements both i1 and i2, I thought InterfaceTest implemented only i2 interface so...
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:IMHO

"If a class implements two interfaces that have default methods with the same name and signature, the compiler will throw an error."

It should be
""If a class implements two interfaces that have default methods with the same signature, the compiler will throw an error."  since method signature comprises name and parameter type(if any)

JLS 10, 8.4.2. Method Signature wrote:Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any)

An interesting thing is that even in some places JLS used name as well as signature simultaneously. Here is one of them.

JLS 10, Example 8.1.5-3, 2nd example explanation wrote: the method getNumberOfScales in class Tuna has a name, signature, and return type that matches the method declared in interface Fish and also matches the method declared in interface Piano...

Appears both are correct.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zach Rode wrote:So this is a bit confusing to me. How does the compiler know youre calling i2's method? Is it because you are implicitly inheriting i1 regardless of if you don't state it? It seems to me that if you declare both interfaces your code would inherit both methods unlike if you only implemented i2, then your class would inherit the overriden version of the method.

-Zach



I wouldn't be at all surprised to discover that the compiler ignores the "implements i1" part simply because i2 (as an extension of i1 itself) takes precedence.

Consequently this code is not an example of what the book is talking about.
Remove the "extends i1" from i2 and you'll see it work as stated.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic